Need VBA code to find text until end of document is reached

G

GlennMc

I'm using Word 2003. I have a for next loop to search through indexes { XE "
"} in my document. I'm using a find statement to find the " XE ", store the
string in a variable, compare it to the next found string, and delete if it
is a duplicate. The number of indexes in the document will vary. How can I
have the code loop until the end of the document is reached. I don't know
the syntax to use, if I can create a range based on words or indexes, or
count the number of indexes first and then use a for next loop.
 
G

Greg

GlennMc.

You should be able to use .Wrap = wdFindStop.

Here is an example:

Sub Test()
Dim orng As Range
Set orng = ActiveDocument.Range
With orng.Find
.Text = "some text"
.Wrap = wdFindStop
Do While .Execute
If .Found Then
orng.Font.Color = wdColorBlue
End If
Loop
End With
End Sub
 
D

Dave Lett

Hi Glenn,

You might find your task easier if you use the fields collection, as in the
following example:

Dim iFld As Integer
Dim sCurField As String
Dim sPrevField As String
sPrevField = ""
For iFld = ActiveDocument.Fields.Count To 1 Step -1
If ActiveDocument.Fields(iFld).Type = wdFieldIndexEntry Then
sCurField = ActiveDocument.Fields(iFld).Code
If sCurField = sPrevField Then
ActiveDocument.Fields(iFld).Delete
End If
sPrevField = ActiveDocument.Fields(iFld).Code
End If
Next iFld

HTH,
Dave
 
G

GlennMc

Very slick Dave. Thanks.
--
GlennMc


Dave Lett said:
Hi Glenn,

You might find your task easier if you use the fields collection, as in the
following example:

Dim iFld As Integer
Dim sCurField As String
Dim sPrevField As String
sPrevField = ""
For iFld = ActiveDocument.Fields.Count To 1 Step -1
If ActiveDocument.Fields(iFld).Type = wdFieldIndexEntry Then
sCurField = ActiveDocument.Fields(iFld).Code
If sCurField = sPrevField Then
ActiveDocument.Fields(iFld).Delete
End If
sPrevField = ActiveDocument.Fields(iFld).Code
End If
Next iFld

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