Solved

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

Posted on 2015-02-24
7
3,612 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 51

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 51

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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Technology Partners: 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!

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

690 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