Take the 2-minute tour ×

does anyone have a VBA code to create a bookmark into the first column of an MSWord table?

Let's say I have a table looking like this

. ref . Title          .
.  1  . Title 1        .
.  2  . Title 2        .
. foo . Title 3        .
. bar . Title 4        .

and I want a VBA code fragment that creates a bookmark named "T1_1" on the string "1" in row 2 / column 1, and bookmarks named "T1_2", "T1_foo" and "T1_bar" on the strings in the other cells of column 1.

I don't mind to hardcode the prefix "T1" (and substitute for other tables each time). I don't mind to select tables before running the macro, I don't mind giving those cells a special format, and I don't mind to get a superfluous bookmark "T1_ref" from the first row - so the code doesn't need to distinguish between table title and table row.

Thanks a lot in advance

share|improve this question

1 Answer 1

up vote 2 down vote accepted
Public Sub Testing()
    Dim strText As String
    Dim i As Integer, j as Integer
    For j = 1 To ThisDocument.Tables.Count
        For i = 2 To ThisDocument.Tables(j).Rows.Count
            strText = StripNonPrint(ThisDocument.Tables(j).Cell(i, 1).Range)
            ActiveDocument.Bookmarks.Add Range:=ThisDocument.Tables(j).Cell(i, 1).Range, Name:="T1_" & strText
        Next i
    Next j
End Sub

Private Function StripNonPrint(ByVal s As String)
  StripNonPrint = Trim(Replace(s, vbCr & Chr(7), ""))
End Function

This should be enough to get you going on your task. This will add a bookmark to every cell in the first column (except the first row) of any table in the current document.

share|improve this answer
+1. Good answer! –  Todd Main Jun 7 '10 at 18:54
cool thanks ... just minor adjustments to be made: add j to dim, replace "j" for "1" in all "for i ..." loop statements and refine the strip function, change "for j=0" to "for j=1" - that was it .... thanks again –  MikeD Jun 7 '10 at 19:08

Your Answer


