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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.