Find & copy macro--possible?

C

Carrie Downes

I'm wondering if it's possible to create a macro that will search a word
document for any foreign characters with diacritics, copy them, and export
them to a new document? I don't want to remove the characters from where they
are in the text--I just need to know WHICH characters are being used in a
given text. In other words, I need to figure out a thorough way to search
long documents and note any characters that have accents (acute or grave),
breves, umlauts, macrons, etc. Then I need to be able to see a list of the
results so that I know which diacritics are used. Is this even possible?
 
D

Dave Lett

Hi Carrie,

From the VBA help file topic "Character Set (128-255), I'm gathering that
you need the characters in the range of 192-255 (generally speaking).
Therefore, if I find any of those characters, I should have found what
you're looking for. Therefore, the following might be what you're looking
for:


Dim iChar As Integer
Dim sFound as String
Dim oDoc As Document
Selection.HomeKey Unit:=wdStory
For iChar = 192 To 255
With Selection.Find
.ClearFormatting
.Text = Chr(iChar)
.MatchCase = True
If .Execute Then
Selection.HomeKey Unit:=wdStory
sFound = sFound & Chr(iChar) & vbCrLf
End If
End With
Next iChar
Set oDoc = Documents.Add
oDoc.Range.InsertAfter Text:=sFound

HTH,
Dave
 
C

Carrie Downes

YAY! That's exactly what I needed. Thanks for your help on this, Dave! You
have no idea how much easier you've just made things for me. Much appreciated!

~Carrie
 
C

Carrie Downes

Dave,

Is this macro easily adaptable to finding other text within a document and
pulling it out into another separate document? Which line would I change if I
want to modify it for a related function?

Thanks,
~Carrie
 
D

Dave Lett

Hi Carrie,

Well, it depends on what you mean by "other text". If you are looking for
just a word, then we'll have to modify more than one line, and if you're
looking for specific characters, then it _could_ be done by modifying a
single line of code (this assumes the easiest change that might be looking
for).

Perhaps you could describe how you need it to work now?

HTH,
Dave
 
C

Carrie Downes

I need to search for anything in brackets and then list those in a separate
document, just the same as the diacritics. In other words, I want to search
for any string of characters enclosed in <>. Does this pose a problem because
I need to literally search for text AND the brackets themselves?
 
D

Dave Lett

Hi Carrie,

That will require an entirely different routine.

Dim sBraces As String
Dim oDoc As Document
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "\<*\>"
.MatchWildcards = True
Do While .Execute
sBraces = sBraces & Selection.Text & vbCrLf
Loop
End With
End With

Set oDoc = Documents.Add
oDoc.Range.InsertAfter sBraces
oDoc.Range.Sort

Then, you can use the routine from the article "Delete any paragraph that is
an exact duplicate of the preceding paragraph, using a Selection object" at
http://word.mvps.org/faqs/macrosvba/DeleteParaSel.htm

HTH,
Dave
 
C

Carrie Downes

That worked perfectly! Although I don't see why I'd need to use the routine
for selecting and deleting duplicate paragraphs that you mentioned at the end
of your posting. I really just needed to be able to identify what's in
brackets in a document. These are typesetting codes that I use as an editor
to prepare a document for a compositor, and I need to be sure that I've
listed all those used.

Thanks again. You've been a tremendous help! ~Carrie
 
D

Dave Lett

Hi Carrie,

I suggested the second routine so that you would end up with a _unique_ set
of words that we in brackets, just as the first routine only showed one
instance of each character with diacritics. Sounds like you don't really
need it though.

Dave
 
C

Carrie Downes

Hi Dave,

Actually...I think it may be better this way. Since this macro shows every
occurrence (and groups them together, no less!), it serves an extra
function--it allows me to check those codes that need to occur in pairs. Many
of our typesetting codes are similar to HTML in that they need opening and
closing codes. So by running the macro, not only can I see that <CA> is used
(meaning chapter author), but also that there is a corresponding </CA> to
close it. This helps tremendously because I can make sure that there aren't
any stray open codes left without a closing code. So thanks!

However, if I did want to modify the first routine (for identifying the
diacritics used) and wanted to do the same for identifying the bracket codes
used (without listing every instance), I'm just curious--how would you do it?
It is possible to modify the first macro?
 
D

Dave Lett

Hi Carrie,

You _could_ modify the first routine to do something similar. However, it's
more work than it's worth. That is, you'd have to define each unique set of
brackets that you wanted to find and place them in an array. Then you would
cycle through the array, finding each instance (or not finding it).

Dim aBraces
Dim iBraces As Integer
Dim sFound As String
Dim oDoc As Document

aBraces = Array("<brace1>", "<brace2>", "<brace3>") '''and so on

Selection.HomeKey Unit:=wdStory
For iBraces = 0 To UBound(aBraces)
With Selection.Find
.ClearFormatting
.Text = aBraces(iBraces)
.MatchCase = True
If .Execute Then
Selection.HomeKey Unit:=wdStory
sFound = sFound & Chr(iChar) & vbCrLf
End If
End With
Next iBraces
Set oDoc = Documents.Add
oDoc.Range.InsertAfter Text:=sFound

I haven't tested this. I just wrote it up to show that it'd really be less
efficient. For example, it wouldn't find typos in your mark up because you
have to specify exactly which braces to look for, instead of having Word
return all that are in the document.

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top