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><"

Re: How to stop looping search at end of document? by Jezebel

Jezebel
Thu Jul 01 02:16:07 CDT 2004

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>
<





"Jon" <jonjermey@optusnet.com.au> wrote in message
news:40e3b4ad$0$25463$afc38c87@news.optusnet.com.au...
> 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><"
>
>



Re: How to stop looping search at end of document? by sjac

sjac
Thu Jul 01 08:41:54 CDT 2004

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

Re: How to stop looping search at end of document? by Jon

Jon
Fri Jul 02 19:01:55 CDT 2004

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" <dwarves@heaven.com.kr> wrote in message
news:OLJWJtzXEHA.2868@TK2MSFTNGP09.phx.gbl...
> 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>
> <
>
>