×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
Hi there,

I use vba code from an ms access code module to format bookmarks in a word document.

Therefore I use the following code:

doc.Bookmarks("mv5_5").Select
Selection.Font.ColorIndex = wdRed
Selection.Font.Bold = True

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

There is no reason to work with selection. Rather, work directly with the .range object. For example.

activedocument.bookmarks("InTableMark").Range.Font.Bold=true

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

(OP)
Thanx for all the help!

@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

marcello62,
Don't forget to award a star(s) for help received. Click on Great Post! link in helpful post(s).


---- Andy

There is a great need for a sarcasm font.

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

If you need to expand the range of a bookmark

CODE --> VBA

With MyDocument
    BeginRange = .Bookmarks("Mark1").End
    EndRange = .Bookmarks("Mark2").Start
    Set MyBookmarksRange = .Range(BeginRange, EndRange)
End with 

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close