0

I am trying to find out which table cell my bookmarks are in in my Word document. I have no problem looping through the bookmarks, that was pretty simple. Now, I am trying to identify the table cell the bookmark is in, but I'm having a tough time doing this.

Alternatively, is there a way to bind data to a bookmark (like using an enclosed bookmark), which can then be referenced and copied to another document? I can't use enclosed bookmarks as the text in the cells needs to change frequently and users don't want to have to bookmark the new text each time.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AutoM8R
  • 3,020
  • 3
  • 32
  • 52
  • I actually solved this going another route: instead of looping through bookmarks then finding the table cell, I am looping through the table cells and finding the bookmarks. It's quite easy this way, I don't know if it's possible the other way. FYI for anyone else who comes across this. – AutoM8R Sep 11 '12 at 16:13
  • Why don't you post your solution as an answer? – Olle Sjögren Sep 11 '12 at 16:51
  • Didn't know that was possible, I am posting the code now. – AutoM8R Sep 11 '12 at 19:14

3 Answers3

1

I used RowIndex and ColumnIndex as provided in this link -

Word VBA - How to locate table cell containing Content Control?

Dim bkRange As Range
Dim rIndex, cIndex As Integer
Set bkRange = ActiveDocument.Bookmarks(bookmarkName).Range
rIndex = bkRange.Cells(1).RowIndex
cIndex = bkRange.Cells(1).ColumnIndex

Once you get these values, you can access the cell of table like -

ActiveDocument.Tables(1).Cell(rIndex, cIndex)
0

As mentioned above, a better way to do this is to go through the table, find the bookmarks. My script below goes through the second column of the first table, looking for all bookmarks. The bookmarks it finds correspond to bookmarks I've setup in the other document "Document to Populate.docx". Data from the ActiveDocument is populated into the second document wherever a bookmark is found as follows:

Sub AutomateQuestionnaire2()
' Prototype 2
' Different approach was used, instead of looping through bookmarks, loop
' through the tables looking for bookmarks. This method is more flexible
' and  is better suited for our Word documents which always include tables.
' Limitations: Bookmark needs to be in both documents using the same ID, and
' data must be in a table, column 2.

Dim oRow As Row
Dim oRange As Range
Dim oFindRange As Range
Dim oBookmark As Bookmark
Dim oQuestionnaire As Word.Document
Dim oApp As Word.Application

Dim strFilePath As String
Dim strText As String

strFilePath = ActiveDocument.Path

'Open the second to populate it
Set oApp = New Word.Application
oApp.Visible = True
Set oQuestionnaire = oApp.Documents.Open(strFilePath + "\Document to Populate.docx")

'We'll loop through each row of the table looking for bookmarks, if a bookmark is found
'the text adjacent to that bookmark, in the table cell, will be copied to the same
'bookmark if found in the questionnaire.
For Each oRow In ActiveDocument.Tables(1).Rows
    'Limits the range to the middle column as is the case for the ITGC 532 form
    Set oRange = oRow.Cells(2).Range
    Set oBookmark = oRange.Bookmarks(1)

    'VBA will terminate the script if it comes across an error, instead
    'let's add some error handling to skip errors.
    On Error GoTo SkipToNext

    strText = oRange.Text
    oQuestionnaire.Bookmarks(oBookmark).Range.Text = strText

    'Find the newly inputted text and differentiate it (bold for now)
    Set oFindRange = oQuestionnaire.Content
    oFindRange.Find.Execute FindText:=strText, Forward:=True
    If oFindRange.Find.Found = True Then oFindRange.Font.ColorIndex = wdBlue

SkipToNext:

Next
AutoM8R
  • 3,020
  • 3
  • 32
  • 52
  • Some of the comments in the code up there are not perfect because I as tweaking this as I posted it. Questionnaire is what I'm trying to populate. – AutoM8R Sep 11 '12 at 19:21
0

There is no need to loop through the tables, let alone their rows & columns. Here's some significantly simpler code for you to play with:

Sub TestBookMark(BkMkNm As String)
Dim Rng As Range
With ActiveDocument
  Set Rng = .Range(0, 0)
  With .Bookmarks(BkMkNm).Range
  If .Information(wdWithInTable) = True Then
    Rng.End = .End
    MsgBox "Bookmark: " & BkMkNm & vbTab & "Table: " & Rng.Tables.Count & vbTab & "Row: " & .Cells(1).RowIndex & vbTab & "Column: " & .Cells(1).ColumnIndex
  End If
  End With
End With
End Sub

which you could call with code like:

Sub Demo()
Call TestTable("BkMk")
End Sub

Obviously, a loop through the bookmarks could be implemented to perform multiple tests. That should be far more efficient than testing each table/cell.

macropod
  • 12,757
  • 2
  • 9
  • 21