For Loops with variables

J

Joanne

Hello,
I would like to create a loop where the name of a bookmark changes with each
loop. All I can find about For Loops is about incrementing a number or a
collection. I just want the name of the bookmark to change with each loop
but the information being added is the same. Is there a way to do this?

Thank you.
 
D

Doug Robbins - Word MVP

Dim i as Long
With ActiveDocument
For i = 1 to .Bookmarks.Count
.Bookmarks(i).Range.InsertBefore "Your Text"
Next i
End WIth

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Joanne

Hi,
Thank you so much for your answer. The only thing is, it's not all the
bookmarks in the document that I need, only a select few. Is it possible to
select only the ones I need? Thank you in advance.
 
G

Gordon Bentley-Mix

Joanne,

Perhaps something like this would work:

Sub Test()
Dim myArray() As String
Dim i As Long
Dim myBookmarkName As String
myArray = Split("Bookmark1|Bookmark3|Bookmark6|Bookmark12", "|")
For i = 0 To UBound(myArray)
myBookmarkName = myArray(i)
DoBookmarkOps myBookmarkName
Next i
End Sub

Sub DoBookmarkOps(myBookmarkID As String)
With ActiveDocument
If .Bookmarks.Exists(myBookmarkID) Then
With .Bookmarks(myBookmarkID)
'*** Do stuff with this bookmark ***
End With
End If
End With
End Sub

In this construction, you create an array of bookmark names and, using the
upper bound of this array as the control for the "For" loop, make a series of
calls to a procedure that accepts a bookmark name (sourced from the array and
located by the index from the "For" loop) to perform the repetitive
operations with the bookmark.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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