I have this problem with a loop for filling text into bookmarks in a MS Word document. It seems that the loop keeps restarting everytime and never gets to the 'next' bookmark.
Here is the code (the reason I use the .StartsWith
is that I have several bookmarks that need to be filled with the same text):
For Each bm As Word.Bookmark In oDoc.Bookmarks
Select Case True
Case bm.Name.StartsWith("A")
Call UpdateBookmarks(oDoc, bm.Name, TextA)
Case bm.Name.StartsWith("B")
Call UpdateBookmarks(oDoc, bm.Name, TextB)
Case bm.Name.StartsWith("C")
Call UpdateBookmarks(oDoc, bm.Name, TextC)
'etc...
End Select
Next
As said, the loop keeps restarting on the first bm in the Collection. If I do:
For Each bm As Word.Bookmark In oDoc.Bookmarks
MsgBox(bm.Name)
Next
The loop works as it should (I get the bookmark names for all the bookmarks). The called sub looks like this:
Public Sub UpdateBookmarks(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByVal BookmarkName As String, ByVal Text As String)
Dim BMrange As Word.Range = Doc.Application.ActiveDocument.Bookmarks(BookmarkName).Range
BMrange.Text = Text
Doc.Application.ActiveDocument.Bookmarks.Add(Name:=BookmarkName, Range:=BMrange)
End Sub
Any ideas why the first loop isn't working properly?