Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

Create bookmarks in the only table of the active document using VBA

Dear Experts:

I got a 2 column table in the currently active document. The number of rows can vary from
1 to n rows depending on the currently active document.

I would like to run a macro that ...
... creates bookmarks of the cell values in the second column of the only table in the currently active document.
... The macro is to run thru the table from top to bottom. The bookmarks are to be named:

bookmark_01
bookmark_02
bookmark_03
bookmark_nn

Example sample table

Column 1    Column 2

17                Surgery Department
13                Oncology Department
18                Pediatrics Department

So the following bookmarks should be created

bookmark_01 = Range of Text marked by the bookmark: Surgery Department
bookmark_02 = Range of Text marked by the bookmark: Oncology Department
bookmark_03 = Range of Text marked by the bookmark: Pediatrics Department

The range of text marked by the bookmark is not to include the cell end marker

Help is much appreciated, thank you very much in advance.

Regards, Andreas
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

I'm not sure why you specifically do not want to bookmark the whole cell but I think this is what you need, step through it to see what happens with the selection.
Sub macroAddBookmarks()
    With ActiveDocument.Tables(1)
        Dim rw As Long
        For rw = 1 To .Rows.Count
            .Rows(rw).Cells(2).Select
            Selection.MoveLeft wdCharacter, 1, wdExtend
            ActiveDocument.Bookmarks.Add "bookmark_" & Format(rw, "00"), Selection.Range
        Next
    End With
End Sub

Open in new window

If the list is long, you could use Application.ScreenUpdating to make the macro run faster.
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andreas Hermle

ASKER

Dear Robert and Graham,

thank you very much for your professional help. Both codes work just fine although I have to admit that Graham's code is more elegant since it does not use the selection object.

There is one thing I would like to get tweaked. There are cases where the only table in the document has column headers hence in these cases ...
the bookmarks should only be created from row two (2) down to the last row  (in the second column) with the first bookmark still being named 'bookmark_01'

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
This will omit the heading rows:
Sub AddBookmarks()
    Dim tbl As Table
    Dim rw As Row
    Dim rng As Range
    Dim bmk As Bookmark
    Dim cl As Cell
    Dim doc As Document
    
    Set doc = ActiveDocument
    Set tbl = doc.Tables(1)
    
    For Each rw In tbl.Rows
        If Not rw.HeadingFormat Then
            Set cl = rw.Cells(2)
            Set rng = GetCellTextRange(cl)
            strBookmarkName = "bookmark_" & Format(cl.RowIndex, "00")
            Set bmk = doc.Bookmarks.Add(strBookmarkName, rng)
        End If
    Next rw
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Robert,
Your version runs OK in Word 2003 and in Word 2007 on my Win 7 system
Dear both,

thank you very much for your quick and professional help.

Robert, nice tweaking of Graham's code. Yes you are right, with Graham's tweaked code the bookmarks do not start with _01 anymore, but by changing line 16 on Graham's code (see below) the numbering is fine again (adding -1). I am sure it  was just an oversight on Graham's part.  

Line 16: strBookmarkName = "bookmark_" & Format(cl.RowIndex - 1, "00")

I also tweaked your first code on line 4 and 7 to get the desired result.

Sub macroAddBookmarks_Robert_tweaked_by_Andreas_Hermle()
    With ActiveDocument.Tables(1)
        Dim rw As Long
        For rw = 2 To .Rows.Count
            .Rows(rw).Cells(2).Select
            Selection.MoveLeft wdCharacter, 1, wdExtend
            ActiveDocument.Bookmarks.Add "bookmark_" & Format(rw - 1, "00"), Selection.Range
        Next
    End With
End Sub

Open in new window


Nevertheless you did a very fine job on tweaking Graham's code. I am aware that the following property of the first row, the header row, must be set as follows: Repeat as header row at top of each page. Otherwise the codes will not work.

I suggest splitting the points 50:50 because both of you contributed major parts.

thank you very much for your great help. I really appreciate it.

Regards, Andreas
Great job from both of you. Regards, Andreas