views:

1010

answers:

1

I am using Word Automation to manipulate some documents (yuk!). I am using this command to get a bookmark within a document.

Object oBookmark = "MyBookmarkName";
Range oRngoBookmark = wordDocument.Bookmarks.get_Item(ref oBookmark).Range;

What I want to find out is if that bookmark is in a table in the document or not. Is there an easy way to do that?

I am calling this code from C# in a .NET application.

+1  A: 

I'm not certain how this would work out with .net, but here's a macro that exhibits the idea. The object model tends to be the same so I don't think it will be hard to translate to C# from VBA.

Sub BookmarksInTables()
    Dim aTable As Table
    Dim aBookmark As Bookmark

    For Each aBookmark In ActiveDocument.Bookmarks
        For Each aTable In ActiveDocument.Tables
            'If start of book mark is inside the table range or
            ' the end of a book mark is inside the table range then YES!
            If (aBookmark.Range.Start >= aTable.Range.Start _
                And aBookmark.Range.Start <= aTable.Range.End) _
            Or (aBookmark.Range.End >= aTable.Range.Start _
                And aBookmark.Range.End <= aTable.Range.End) Then
                MsgBox aBookmark.Name + " is inside a table"
            Else
                MsgBox aBookmark.Name + " is not inside a table"
            End If
        Next
    Next
End Sub

It works by checking if the bookmark's start or end is inside each table's range (in the entire document). It will work if any part of the bookmark is inside of a table.

Alternatively if you're needing to find out if the bookmark is exclusively inside a table you would want to check for the bookmark's start to be greater than or equal to the table's start and the bookmark's end to be less than or equal to the table's end.

--Kris

KSimons
Nice one. I will give it a go (after lunch).
Craig