Solved

sequentially bookmark cells in all tables using VBA

Posted on 2013-05-14
6
374 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
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 the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

624 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