VBA - Word, Insert buildingblocks without deleteing the bookmark

Hi, is there a way to insert a buldingblock without deleting the bookmarks?

The building block will be inserted from a userform with a checkbox.

It would be prefereable to do it like this with the bookmarks:

Private Sub cmdOK_Click()

    InsertBookmark "test4", TextBox1.Value

End Sub

Sub InsertBookmark(strBookmark As String, TextToUse As String)
    
'Insert text into bookmark without deleting
    Dim BMRange As Range
    
    Set BMRange = ActiveDocument.Bookmarks(strBookmark).Range
        BMRange.Text = TextToUse
        
    If Len(strBookmark) < 0 Then ActiveDocument.Bookmarks.Add strBookmark, BMRange
    'ActiveDocument.Bookmarks.Add strBookmark, BMRange
    
End Sub

Open in new window

LVL 1
HakumAsked:
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.

Rgonzo1971Commented:
Hi,

Why don't you use

Sub InsertBookmark(strBookmark As String, TextToUse As String)
    
'Insert text into bookmark without deleting
    Dim BMRange As Range
    
    Set BMRange = ActiveDocument.Bookmarks(strBookmark).Range
        BMRange.Text = TextToUse
        
    ActiveDocument.Bookmarks.Add strBookmark, BMRange
    
End Sub

Open in new window

Regards
0
GrahamSkanRetiredCommented:
Hi Harsh,
You seem to understand that bookmarks are deleted when the range is overwritten and have to be re-inserted afterwards. The same principle applies when the are overwritten by the action of inserting from a building block.

Private Sub cmdOK_Click()
    Dim Templ As Template
    Set Templ = Templates(1)
    InsertBuildingBlock "MyBookmark", "MyBuildingBlock", Templ
End Sub

Sub InsertBuildingBlock(strBookmarkName As String, strBuildingBlockName As String, SourceTemplate As Template)
    
'Insert text into bookmark without deleting
    Dim BMRange As Range
    
    Set BMRange = ActiveDocument.Bookmarks(strBookmarkName).Range
    SourceTemplate.BuildingBlockEntries(strBuildingBlockName).Insert BMRange, True
    ActiveDocument.Bookmarks.Add BMRange, strBookmarkName
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
HakumAuthor Commented:
I'm soo sorry guys for asking the same question over and over again, I seriously dont understand how to do it and what to do about this function I want...

Is it possible to hire one of you for a cheap price to get exactly what i want it to do? I guess i'm causing more frustration then any good...
0
GrahamSkanRetiredCommented:
Daniel Mitchell points out that line 14 in the macro
    ActiveDocument.Bookmarks.Add BMRange, strBookmarkName

Open in new window

is wrong with the arguments in the wrong order. It should be
    ActiveDocument.Bookmarks.Add strBookmarkName, BMRange

Open in new window

Thank you Daniel
1
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.