VBA Append and Remove building blocks to the start of a bookmark

How can I append a quick part/building block to the start of a bookmark in VBA and keep a reference to it in a list for removal or hiding later?
ag1iffitAsked:
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:
I suggest that you insert the text into the bookmark, so that the bookmark can later be used to find the inserted text. You can use a collection to keep a list of which building block is in each bookmark
    Dim doc As Document
    Dim rng As Range
    Dim MyTemplate As Template
    Dim coll As New Collection
    
    '...
    'template and doc objects set here
    '...

    With doc
        Set rng = doc.Bookmarks("MyBookmark").Range
        MyTemplate.BuildingBlockEntries("MyBlock").Insert rng, True
        doc.Bookmarks.Add "MyBookmark", rng 're-add bookmark to ensure that it defines the new text
        coll.Add "MyBlock", "MyBookmark"
    End With
    
    MsgBox "Mybookmark contains " & coll("MyBookmark")

Open in new window

0
GrahamSkanRetiredCommented:
With that (and your apologetic message) in mind, yes, you could enclose new insertions into the bookmark. If you only concerned with one bookmark, you could do it like this:

   
Dim doc As Document
    Dim rng As Range
    Dim rng1 As Range
    Dim MyTemplate As Template
    Dim strContainedBlocks() As String
    Dim i As Integer
    Dim j As Integer
    Dim strMsg As String
    '...
    'template and doc objects set here
    '...
    With doc
        Set rng = doc.Bookmarks("MyBookmark").Range
        Set rng1 = rng.Duplicate
        rng.Collapse wdCollapseEnd
        MyTemplate.BuildingBlockEntries("MyBlock").Insert rng, True
        rng.Start = rng1.Start
        doc.Bookmarks.Add "MyBookmark", rng
        ReDim Preserve strContainedBlocks(i)
        strContainedBlocks(i) = "MyBlock"
        i = i + 1
        
        Set rng = doc.Bookmarks("MyBookmark").Range
        Set rng1 = rng.Duplicate
        rng.Collapse wdCollapseEnd
        MyTemplate.BuildingBlockEntries("MyNewBlock").Insert rng, True
        rng.Start = rng1.Start
        doc.Bookmarks.Add "MyBookmark", rng
        ReDim Preserve strContainedBlocks(i)
        strContainedBlocks(i) = "MyNewBlock"
        i = i + 1
   End With
   strMsg = "Mybookmark contains "
    For j = 0 To i - 1
        strMsg = strMsg & strContainedBlocks(j) & ", "
    Next j
    MsgBox strMsg

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

From novice to tech pro — start learning today.