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
Microsoft Word

Avatar of undefined
Last Comment
Andreas Hermle

8/22/2022 - Mon
Robert Schutt

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
GrahamSkan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Andreas Hermle

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
GrahamSkan

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
GrahamSkan

Robert,
Your version runs OK in Word 2003 and in Word 2007 on my Win 7 system
ASKER
Andreas Hermle

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
ASKER
Andreas Hermle

Great job from both of you. Regards, Andreas
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.