Link to home
Start Free TrialLog in
Avatar of Harsh Kumar
Harsh KumarFlag for Denmark

asked on

VBA - Word, Clear content of bookmark but not the bookmark.

Hi guys,

I have this code and i want it only to delete the content within the bookmark not the bookmark it self,

could anyone help me please:

Public Sub Deletebookmark(strBookmark As String, chkResult As Boolean)

'Delete choosen bookmark
        
    If chkResult = False Then
        ActiveDocument.Bookmarks(strBookmark).Range.Delete
    End If

End Sub

Open in new window


And it gets call by this:

Deletebookmark "test", CheckBox14.Value ' Test BM

Open in new window


I have this code which works for me to replace content within the bookmark.

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

Open in new window


What i want to achive is basically add a placeholder so i can add/remove Buildingblock/quickpart without the bookmarks getting deleted.. is this possible?

I have alot of bits of code i just need some help with doing it right i guess..

How i add the Building block is like this:

If CheckBox24 = True Then ' insert BM
    Set tmpl = ActiveDocument.AttachedTemplate
        tmpl.BuildingBlockEntries("test3").Insert _
        ActiveDocument.Bookmarks("test3").Range, True

Open in new window


I can really figur out how to use the "InsertBookmark" sub with the buildingblock insert

I still learning VBA on my own so please bare with me...

Thank you in advance
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

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

Open in new window

Regards
Avatar of Harsh Kumar

ASKER

I just added some more text to the question please read.

What i'm trying to achieve is being able to add/remove buildingblocks from the document without the bookmarks being deleted, and all this happends if a checkbox is true or false in  the userform.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not working :S

User generated image
There is nothing wrong with Rafael's code.

I have seen that happen.  I think that it is usually part of a table, but I can never reproduce it at will. To be sure we would need to see the part of the document in question.

I suppose that you are trying to implement the code for your previous question: https://www.experts-exchange.com/questions/28618302/VBA-Word-building-up-document-with-buildingblocks-with-userform.html

The method that I recommended doesn't try to keep the bookmark for a deleted  block, but looks for the right place in the bookmarked blocks that are in the document.
Hi guys,

I'm trying to work out a way from my previous question yes, but i'm using the code from the previous question in another document i'm working on, i was basically trying to use the same code in this one but i'm unsure of how to create the index as you told me to, so i was trying to find a workaround to do it in another way thats why this question.

Anyways, the document is attached

Please advise me what to do... im not good at VBA at all so trying to learn,
list-250215-Sample.dot
Harsh,
This question has been answered by RGonzo1951, so you should close it, accepting his answer.
 I will be adding some more comments in your previous question.