Bookmark 1 column of MSWord table

Does anyone have VBA code to bookmark the first column of the MSWord table?

Say I have a table similar to this

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

and I need a VBA code snippet that creates a bookmark with the name "T1_1" in row "1" in row 2 / column 1, and bookmarks with the names "T1_2", "T1_foo" and "T1_bar" in rows to other cells in column 1.

I don't mind hard coding the prefix "T1" (and replacing other tables every time). I don’t mind choosing tables before running the macro, I don’t mind that these cells are in a special format, and I don’t mind getting an extra tab “T1_ref” from the first row, so the code does not need to distinguish between the table header and the table row.

Thank you very much in advance

+4
source share
1 answer
 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 

That should be enough for you to complete your task. This will add a bookmark to each cell in the first column (except the first row) of any table in the current document.

+4
source

Source: https://habr.com/ru/post/1312102/


All Articles