Avatar of Harsh Kumar
Harsh Kumar
Flag for Denmark asked on

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

Microsoft WordMicrosoft OfficeVBA

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
Rgonzo1971

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
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Harsh Kumar

ASKER
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...
GrahamSkan

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23