Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sequentially bookmark cells in all tables using VBA

Posted on 2013-05-14
6
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1200 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 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Suggested Courses

670 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