How to stop looping search at end of document?

J

Jon

I have a macro which is designed to find an occurrence of a string of text,
copy it to a variable, and add some XML tags containing that variable. I
then want it to repeat the process down the document to find the next
occurrence and so on. How can I write a loop that will stop when it reaches
the last occurrence in the document?

The part that I want to loop follows:

Thanks,

Jon.


Selection.Find.ClearFormatting
With Selection.Find
.Text = "<title>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Extend
Selection.Extend Character:="<"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Title = LCase(Selection)
Selection.Collapse (wdCollapseEnd)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="<indexterm><index1>"
Selection.InsertBefore Title
Selection.Collapse (wdCollapseEnd)
Selection.TypeText Text:="</index1><index2>"
Selection.InsertBefore Keyword
Selection.Collapse (wdCollapseEnd)
Selection.TypeText Text:="</index2></indexterm><"
 
J

Jezebel

As I understand it, you're looking for "<title>[Any text]<" and replacing
with <title>[Any text]<indexterm><index1>[any
text]</index1><index2>[Keyword]</index2></indexterm><

Find and replace will do this in one go. Switch on wildcards, search for:
\<title\>([!\<]*)\<

Replace with:
<title>\1]<indexterm><index1>\1index1><index2>[Keyword]</index2></indexterm>
<
 
J

Joseph

Hi Jon,

Try the below code.


Selection.Find.ClearFormatting
With Selection.Find
.Text = "<title>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do While Selection.Find.Execute

If Selection.Find.Found = True Then

Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Extend
Selection.Extend Character:="<"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Title = LCase(Selection)
Selection.Collapse (wdCollapseEnd)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="<indexterm><index1>"
Selection.InsertBefore Title
Selection.Collapse (wdCollapseEnd)
Selection.TypeText Text:="</index1><index2>"
Selection.InsertBefore Keyword
Selection.Collapse (wdCollapseEnd)
Selection.TypeText Text:="</index2></indexterm><"


Else
Exit Do
End If

Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop



hth,
Stephen
http://crosssoft.netfirms.com
 
J

Jon

Thanks for this! I can use it IF there is also a way to select everything
within <index1> and <index2> tags and convert it to lower case. Otherwise I
will have to go the long way round. Any ideas? (Lcase(\1) doesn't work)

Here's the slightly tweaked version I ended up with. Note that 'Keyword' is
a variable.

Jon.


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\<title\>([!\<]*)\<"
.Replacement.Text = _
"<title>\1<indexterm><index1>\1<index1><index2>" & Keyword &
"</index2></indexterm><"
.Forward = True
.Wrap = wdFindStop




Jezebel said:
As I understand it, you're looking for "<title>[Any text]<" and replacing
with <title>[Any text]<indexterm><index1>[any
text]</index1><index2>[Keyword]</index2></indexterm><

Find and replace will do this in one go. Switch on wildcards, search for:
\<title\>([!\<]*)\<

Replace with:
 

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