T
Tony Logan
I'm using a macro to add bookmarks to a document. I wrote the macro to find
each section break in the document, then look for the first alphanumeric
character and set a bookmark.
My problem is that whenever the macro reaches the end of the document, it
starts over at the beginning--the old infinite loop. I've tried the below
coding a couple of different ways--"Do" with Loop Until Selection.Find.Found
= False" and also "NextBookmark:" with "GoTo NextBookmark", but neither will
get me out of the loop.
I'm pretty certain the problem lies in the first instance of .Wrap =
wdFindContinue, but wdFindStop only works once, setting only one bookmark and
then kicking me out of the loop.
Any ideas how to modify this to kick me out of the loop when I reach the end
of the document? Here's the code:
Do
With Selection.Find
.Text = "^b" ' finds section break
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
'.Wrap = wdFindStop 'commented this after trying it; it didn't work
.Format = False
.MatchCase = True
.MatchWildcards = False
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
With Selection
.Collapse (wdCollapseEnd)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A-z0-9]" 'finds alphanumeric character
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWildcards = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="bk" & intbk
.ShowHidden = False
End With
intbk = intbk + 1
Else: Exit Sub
End If
End With
End If
Loop Until Selection.Find.Found = False
each section break in the document, then look for the first alphanumeric
character and set a bookmark.
My problem is that whenever the macro reaches the end of the document, it
starts over at the beginning--the old infinite loop. I've tried the below
coding a couple of different ways--"Do" with Loop Until Selection.Find.Found
= False" and also "NextBookmark:" with "GoTo NextBookmark", but neither will
get me out of the loop.
I'm pretty certain the problem lies in the first instance of .Wrap =
wdFindContinue, but wdFindStop only works once, setting only one bookmark and
then kicking me out of the loop.
Any ideas how to modify this to kick me out of the loop when I reach the end
of the document? Here's the code:
Do
With Selection.Find
.Text = "^b" ' finds section break
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
'.Wrap = wdFindStop 'commented this after trying it; it didn't work
.Format = False
.MatchCase = True
.MatchWildcards = False
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
With Selection
.Collapse (wdCollapseEnd)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A-z0-9]" 'finds alphanumeric character
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWildcards = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="bk" & intbk
.ShowHidden = False
End With
intbk = intbk + 1
Else: Exit Sub
End If
End With
End If
Loop Until Selection.Find.Found = False