This works fine for most of the bookmarks, but for bookmarks within a table it does not work.
and I get the error code "462, the remote server machine does not exist or is unavailable".
When I use the resume next statement in the error handler the code runs, but the bookmarks in the table
are not formatted as specified by above code.

So my question is how to programmatically access bookmarks within the cells of a table.
Any help will be greatly appreciated.

RE: how to handle bookmarks in a table in a word document using vba for applications

RE: how to handle bookmarks in a table in a word document using vba for applications

I'd suggest using code like the following for updating such bookmarks:

CODE

Sub UpdateBookmark(wdDoc As Word.Document, SBkMk As String, sngVal As Single, bBold As Boolean)
Dim wdRng As Word.Range
With wdDoc
If .Bookmarks.Exists(SBkMk) Then
Set wdRng = .Bookmarks(SBkMk).Range
With wdRng
.Text = Format(sngVal, "$#,##0.00;-$#,##0.00")
If sngVal < 0 Then
.Font.ColorIndex = wdRed
Else
.Font.ColorIndex = wdAuto
End If
.Font.Bold = bBold
End With
.Bookmarks.Add SBkMk, wdRng
End If
End With
End Sub

And passing the parameters for the document, bookmark name, value and bold/not bold with code like:

CODE

Sub Demo()
Call UpdateBookmark(doc, "mv5_5", 9871.23, True)
End Sub

The above UpdateBookmark code assumes you're using early binding; if not change:
wdDoc As Word.Document
wdRng As Word.Range
to:
wdDoc As Object
wdRng As Object

Note: The problem with using a range object the way mintjulep suggests (and it probably afflicts your selection approach, too) is that the bookmark's range doesn't expand to encompass the added data. Hence, the font attributes don't get applied to the data; just to what is probably an empty bookmark range.

Cheers
Paul Edstein
[MS MVP - Word]

RE: how to handle bookmarks in a table in a word document using vba for applications

@mintjulep: I had noticed before the range method did not work at all, not outside tables and not inside tables.
Then, I noticed that the select method worked, but only outside tables, not inside tables.

But in the end macropod proved that using the range object eventually works within tables using the combination of dynamically passing parameters using his UpdateBookmark method and early binding.

Everything works as it should now, so thanx again!

RE: how to handle bookmarks in a table in a word document using vba for applications

RE: how to handle bookmarks in a table in a word document using vba for applications

Quote:

If you need to expand the range of a bookmark

The code I posted already handles a bookmarked range's expansion and contraction. Moreover, your method doesn't "expand the range of a bookmark"; all it does it set an object that spans the range between two bookmarks (neither bookmark is expanded).

Cheers
Paul Edstein
[MS MVP - Word]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.The Tek-Tips staff will check this out and take appropriate action.