Solved

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

Posted on 2015-02-24
7
2,883 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 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with a moving column in Excel 6 38
Excel formula - data format 5 18
Access VBA to Count Worksheets in a Workbook 3 21
Excel VBA 30 38
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

856 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