Solved

sequentially bookmark cells in all tables using VBA

Posted on 2013-05-14
6
363 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS WORD does not allow desired results. 25 91
Message When Opening Word 4 58
Issues when typing e-mails or Word documents. 10 52
MS Word Formatting 3 47
There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

911 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

22 Experts available now in Live!

Get 1:1 Help Now