Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

marcello62

Technical User
Jun 19, 2006
42
0
0
NL
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.
 
There is no reason to work with selection. Rather, work directly with the .range object. For example.

activedocument.bookmarks("InTableMark").Range.Font.Bold=true
 
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]
 
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!
 
marcello62,
Don't forget to award a star(s) for help received. Click on [blue]Great Post![/blue] link in helpful post(s).


---- Andy

There is a great need for a sarcasm font.
 
If you need to expand the range of a bookmark
Code:
With MyDocument
    BeginRange = .Bookmarks("Mark1").End
    EndRange = .Bookmarks("Mark2").Start
    Set MyBookmarksRange = .Range(BeginRange, EndRange)
End with
 
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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top