Sequentially bookmark expressions in tables by means of VBA

Dear Experts:

I got a multi-section word document.

There is one table on each section and nothing else.

Row 2 and Column 1 of these tables always have an expression like the following:

Catalogue number / (GG Code): 43 487 44 07 / (EN/187)

I would like to sequentially bookmark the sub-expressions (by means of a macro) that are within the second parantheses, i.e. (EN/187) in the above case

Examples:

Table 1, Row 2, Column 1
Catalogue number / (GG Code): 43 487 44 07 / (EN/187)
EN/187 is bookmarked 'bk_001'

Table 2, Row 2, Column 1
Catalogue number / (GG Code): 43 487 42 07 / (EN/188)
EN/188 is bookmarked 'bk_002'

Table 3, Row 2, Column 1
Catalogue number / (GG Code): 43 487 10 07 / (EN/189)
EN/189 is bookmarked 'bk_003'

Table 4, Row 2, Column 1
Catalogue number / (GG Code): 43 504 01 07 / (EN/190)
EN/190 is bookmarked 'bk_004'

etc.

Help is very much appreciated.

Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
This version bookmarks the text within the last pair of brackets in the cell
Sub Catalogue2()
    Dim tbl As Table
    Dim col As Column
    Dim cl As Cell
    Dim rw As Row
    Dim rng As Range
    Dim n As Integer
    Dim p As Integer
    Dim q As Integer
    
    n = 0
    For Each tbl In ActiveDocument.Tables
        n = n + 1
        Set rng = tbl.Cell(2, 1).Range
        p = InStrRev(rng.Text, "(")
        q = InStrRev(rng.Text, ")")
        rng.End = rng.Start + q - 1
        rng.MoveStart wdCharacter, p
        ActiveDocument.Bookmarks.Add "bk_" & Format(n, "000"), rng
    Next tbl
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
This ignores Sections because you say that there is exactly one table per Section
Sub BookmarkCatalogue()
    Dim tbl As Table
    Dim col As Column
    Dim cl As Cell
    Dim rw As Row
    Dim rng As Range
    Dim n As Integer
    n = 0
    For Each tbl In ActiveDocument.Tables
        n = n + 1
        Set rng = tbl.Cell(2, 1).Range
        rng.Collapse wdCollapseEnd
        rng.MoveStart wdCharacter, -8
        rng.MoveEnd wdCharacter, -2
        ActiveDocument.Bookmarks.Add "bk_" & Format(n, "000"), rng
    Next tbl
End Sub

Open in new window

0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

thank you very much for your swift help. Upps, I just saw that there might be expressions in the second parentheses with less or more than  6 characters such as ENZ/443 or DE/43937 or D/43.

Help is much appreciated. Thank you  very much :-)
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

thank you very much for your great support. Will test it tomorrow and then let you know.

have a nice sunday. Andreas
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Graham, great job from your side as always,

Thank you very much for it.

Regards, Andreas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.