Create bookmark into 1st column of MSWord table
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
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.
精彩评论