Solved

sequentially bookmark cells in all tables using VBA

Posted on 2013-05-14
6
361 Views
Last Modified: 2013-05-14
Dear Experts:

I wonder whether the following can be achieved by running a macro:

- loop thru all the tables of the active document

- For each table do the following:
      Go to the second row and bookmark the first and fourth cell sequentially as follows:
           bookmark_1 (first cell of the 2nd row) and bookmark_2 (fourth cell of the 2nd row)

The second table's bookmarks would be as follows:
           bookmark_3 (first cell of the 2nd row) and bookmark_4 (fourth cell of the 2nd row)

The third table's bookmarks would be as follows:
           bookmark_5 (first cell of the 2nd row) and bookmark_6 (fourth cell of the 2nd row)

and so forth.

I hope I could make myself clear.

Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
  • 2
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39163898
Not to say there aren't more efficient ways but for example on the activedocument try:

Sub markit()
Dim tbl As Table
Dim bkm As Bookmark
Dim bolFound As Boolean
Dim intNumber As Integer

    intNumber = 1
    For Each tbl In ActiveDocument.Tables
        bolFound = False
        For Each bkm In tbl.Cell(2, 1).Range.Bookmarks
            If LCase(bkm.Name) = "bookmark_" & intNumber Then
                bolFound = True
                Exit For
            End If
        Next
        If Not bolFound Then ActiveDocument.Bookmarks.Add Name:="bookmark_" & intNumber, Range:=tbl.Cell(2, 1).Range
        
        bolFound = False
        For Each bkm In tbl.Cell(2, 4).Range.Bookmarks
            If LCase(bkm.Name) = "bookmark_" & intNumber + 1 Then
                bolFound = True
                Exit For
            End If
        Next
        If Not bolFound Then ActiveDocument.Bookmarks.Add Name:="bookmark_" & intNumber + 1, Range:=tbl.Cell(2, 4).Range
        intNumber = intNumber + 2
    Next
    
End Sub

Open in new window


Chris
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39163912
Hi Andreas. Try it like this.
Sub AddTableBookmarks()
    Dim tbl As Table
    Dim b As Integer
   
    b = 1
    With ActiveDocument
        For Each tbl In .Tables
            .Bookmarks.Add ("bookmark_" & b), tbl.Cell(2, 1).Range
            .Bookmarks.Add ("bookmark_" & b + 1), tbl.Cell(2, 4).Range
            b = b + 2
        Next tbl
    End With
   
End Sub
0
 

Author Comment

by:AndreasHermle
ID: 39164188
Dear both,

both codes work fine. Thank you very much for it. The trouble is ...

... I forgot to mention that only the text in the cells are to be bookmarked. Sorry about that.

With your code the whole cell including the cell marker get bookmarked and hence if I insert a crossreference in my document refering to the bookmark text, a table cell gets inserted.

Could you please tweak the code so that only the cell's text gets bookmarked.

Thank you very much in advance.

Regards, Andreas
0
A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 total points
ID: 39164274
This avoids the cell delimiter characters which the cell.range property returns
Sub AddTableBookmarks()
    Dim tbl As Table
    Dim b As Integer
    Dim rng As Range
    
    b = 1
    With ActiveDocument
        For Each tbl In .Tables
            Set rng = tbl.Cell(2, 1).Range
            rng.MoveEnd wdCharacter, -1
            .Bookmarks.Add ("bookmark_" & b), rng
            
            Set rng = tbl.Cell(2, 4).Range
            rng.MoveEnd wdCharacter, -1
            .Bookmarks.Add ("bookmark_" & b + 1), rng
            b = b + 2
        Next tbl
    End With
    
End Sub

Open in new window

0
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 200 total points
ID: 39164293
Adapting to Grahams code since the error tests are superfluous ... Try:

Sub AddTableBookmarks()
' note case insensitive on naming
' note relocates an existing bookmark or creates it anew therefore no need to test
Dim tbl As Table
Dim intNumber As Integer
    
    intNumber = 1
    With ActiveDocument
        For Each tbl In .Tables
            .Bookmarks.Add ("bookmark_" & intNumber), ActiveDocument.Range(tbl.Cell(2, 1).Range.Start, tbl.Cell(2, 1).Range.End - 1)
            .Bookmarks.Add ("bookmark_" & intNumber + 1), ActiveDocument.Range(tbl.Cell(2, 4).Range.Start, tbl.Cell(2, 4).Range.End - 1)
            intNumber = intNumber + 2
        Next tbl
    End With
    
End Sub

Chris

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 39164492
Dear Graham + Chris,

thank you very much for your great support. Both codes work just fine.

Regards, Andreas
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now