Solved

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

Posted on 2015-02-24
7
2,095 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

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 48

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now