Solved

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

Posted on 2015-02-24
7
3,170 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:Hakum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40628322
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
0
 
LVL 1

Author Comment

by:Hakum
ID: 40628334
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.
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40628362
hi then try

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

'Delete choosen bookmark
        
    If chkResult = False Then
         Dim BMRange As Range
    
       Set BMRange = ActiveDocument.Bookmarks(strBookmark).Range
        BMRange.Text = ""        
       ActiveDocument.Bookmarks.Add strBookmark, BMRange
    End If

End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Hakum
ID: 40628382
not working :S

Capture.PNG
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40628506
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: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_28618302.htm

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.
0
 
LVL 1

Author Comment

by:Hakum
ID: 40630106
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40630766
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.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question