Bookmarks in VBA?

J

JB

Hi Folks,

How can I loop through the number of bookmarks in a document assigning the
name to a VAR and output it to a file? It's mainly the parsing of the
bookmark name from the bookmark property I need as I can do teh rest from
there.

I can loop through the file holding the names of the bookmark and add them
to a doc but I can't seem to retrieve them!

Cheers
J
p.s. here's what I tried

If ActiveDocument.Bookmarks.Count > 1 Then
BKMCount = ActiveDocument.Bookmarks.Count
ReDim aMarks(BKMCount)
i = 0

For i = 0 To BKMCount Step 1
aMarks = ActiveDocument.Bookmarks.Item(i)
Next i
filename = "C:\test2.txt"
filenumber = FreeFile
Open filename For Input As #filenumber
On Err GoTo Closefile
Do While Not (EOF(filenumber))
Input #filenumber, aMarks(i)
i = i + 1
Loop
Closefile:
Close #filenumber
 
J

JGM

Hi JB,

First, I am no expert, but it seems to me that your line
Redid aMarks(BKMCount)
is declaring an array variable.

Don't you have to use something like
Redid MyArray(1)
MyArray(0) = Variable1
MyArray(1) = Variable2
and so on in order to construct the array?

So why are you using
aMarks = ActiveDocument.Bookmarks.Item(i)
?
Can you explain this to me as I am trying to understand Array variables, I
thought I had finally started to grasp them, then I saw your code!

In case I am right,
Try changing
aMarks = ActiveDocument.Bookmarks.Item(i)
to
aMarks(i) = ActiveDocument.Content.Bookmarks.(i)
or
aMarks(i) = ActiveDocument.Content.Bookmarks.(i).Name
If you don't use the "name property, then, (Can someone correct me if I am
wrong) I think that
aMarks(i) = ActiveDocument.Content.Bookmarks.(i)
returns the name by default.

Another important point:
You code:
i = 0

For i = 0 To BKMCount Step 1
aMarks = ActiveDocument.Bookmarks.Item(i)
Next i
The first time around i = 0, but there cannot be a bookmark number 0 in the
bookmark collection, the first bookmark is number 1.
So, here is my suggestion, from my humble experience:
i = 1

For i = 1 To BKMCount Step 1
aMarks(i) = ActiveDocument.Content.Bookmarks.(i)
Next i
Redid Preserve aMarks(i)(i - 1)

Because the line
Redid aMarks(BKMCount)
defines, for example if BKMCount = 5, an array from 0 to 5, or 6 items. But
there are only 5 bookmarks in the document, so you have to redefine the
array. An alternative would be to define the array, I think, like this:
Redid aMarks(BKMCount -1)
so you would not need to redefine and preserve the array with ReDim
Preserve...

Makes any sense?

Also, since you intend to type the bookmark name in a text file, it might
help to play around with the following idea:
Add a line under
aMarks(i) = ActiveDocument.Content.Bookmarks.(i)
such as
aMarks(i) = aMarks(i) & Chr(13)

HTH
Cheers
 
M

macropod

Hi Johnny,

Try something like:

Sub ListBkMrks()
Dim aBmk As Bookmark
Dim strBmkName As String
If Documents.Count > 0 Then
For Each aBmk In ActiveDocument.Bookmarks
strBmkName = strBmkName & vbCrLf & aBmk.Name
Next aBmk
End If
MsgBox strBmkName
End Sub

replacing the messagebox with your preferrred output
method.

Cheers
PS: Remove NO.SPAM from the above before replying.
 

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