Word VBA: How can I find out if a Bookmark is in a Table Cell?

Posted on 2005-05-14
Last Modified: 2008-02-01
I'm trying to modify a Word-document using COM. Multiple values have to be filled in in a table on multiple rows. I need to be sure that a Bookmark (defined by an advanced user in a Word-template) is in a table, so I can add rows if necessary to the table. How to find out that the bookmark is in a table? And if so, in which table, so I can extend the table?
Question by:Sjef Bosman
    LVL 4

    Accepted Solution


    For Each bkm In Word.ActiveDocument.Bookmarks
       If bkm.Range.Information(wdWithInTable) = True Then
       End If

    Or explicitly:
    If Word.ActiveDocument.Bookmarks("knowvn_bookmark").Range.Information(wdWithInTable) = True Then ...

    (look at the 'Information' property of the 'Range' object. It returns many useful information)
    LVL 46

    Author Comment

    by:Sjef Bosman
    That's a big step into the right direction! Now the second part: "And if so, in which table, so I can extend the table?" Do I have to go through all tables, select them and ask whether the bookmark is within the selection?
    LVL 76

    Assisted Solution

    No, you can use the tables collection in the bookmark's range.
    Hopefully the bookmark is in a table:

    Dim bmk As Bookmark
    Dim rng As Range
    Dim tbl As Table
    Set bmk = ActiveDocument.Bookmarks("bmkMyBookmark")
    Set rng = bmk.Range
    If rng.Tables.Count = 1 Then
        Set tbl = rng.Tables(1)
        MsgBox "Mum, I've got a problem"
    End If

    Expert Comment

    You may add to each Table in your template a bookmark with prefix like "TBL_MyBM" by select all the table when adding the bm.
    the user will open the template and add bm to one of the tables, Now you check all the bm in the document and if the bm is with prefix TBL_ you have all the range of the table and you can search if the user add a bm to it.

    For Each bm in doc.bookMarks
    if Left(bm,4)="TBL_" Then CheckBm(bm.Name)

    sub CheckBm(bm)
    Set oRange = Doc.BookMarks(bm).Range
    If i>0 then
    'the user entered a bookmark


    LVL 46

    Author Comment

    by:Sjef Bosman
    Hi all,

    Working through the muddy waters of MSDN on Word and VBA hasn't been easy, but I found the rest of the clues I was looking for. And I suppose I can find the rest there as well. If not, I'll be back ;)

    Yoss: I'm sorry, the whole point of my question, but maybe I wasn't clear enough, is that I do not make the templates. The advanced user is a user of my application, who creates a template for a document. This template has to contain bookmarks to indicate places where data from my application has to be inserted. I don't want to rely upon the advanced user to include a special bookmark, just for the sake of the application, if it's not necessary at all.

    Thanks all!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now