PDA

View Full Version : Solved: Delete bookmark sentence if bookmark value left blank in userfrom



hunsnowboard
03-21-2009, 12:40 AM
Hi Everyone! I am very new to vba, just started learning, please try to help me if you can. My problem is the following:
I would like to make a userform in word which helps my collegues to process documents much faster. I have many bookmarks in the document, but there are cases when I cannot add any value to the bookmark (through the userform). In this cases I would like the sentence in which the bookmark is to be deleted. So if the bookmark has no value given in the userform (is left blank) then the sentence in which the bookmark is should not appear or it should be deleted from the document. Is it possible to do this? If not could you recommend a solution for this?
Thank you very much in advance and have a nice weekend!

hunsnowboard
03-22-2009, 12:21 AM
There is no one who can help? :(

lucas
03-22-2009, 06:13 AM
Hi hunsnowboard, I am no Word guru but I have been looking at this off and on for a couple of days. I came up with this which searches the doc for bookmarks and if its empty then it deletes the sentence. Let me know if this gets you started in the right direction.

Option Explicit
Sub delEmptyBkmksSentences()
Dim objBookmark As Bookmark
For Each objBookmark In ActiveDocument.Bookmarks()
If objBookmark.Range.Text = "" Then
objBookmark.Range.Select
Selection.Expand Unit:=wdSentence
Selection.Range.Delete
Selection.Range.Collapse wdCollapseEnd
End If
Next
End Sub

Maybe Gerry can show us how to avoid the selection. I couldn't figure out any other way to get a handle on the sentence while dealing with the bookmarks......

In the attachment, the two sentences in red font have empty bookmarks.

hunsnowboard
03-22-2009, 07:08 AM
Hi Lucas! Thank you very much for your help! I will take a look at your solution, but right now I am involved in another urgent project! Thank you for your reply, will try it! Thank you!

mdmackillop
03-22-2009, 09:02 AM
Option Explicit
Sub delEmptyBkmksSentences()
Dim objBookmark As Bookmark
Dim rng As Range
For Each objBookmark In ActiveDocument.Bookmarks()
If objBookmark.Range.Text = "" Then
Set rng = objBookmark.Range
rng.Expand Unit:=wdSentence
rng.Delete
End If
Next
End Sub

lucas
03-22-2009, 10:08 AM
Thanks Malcolm.

macropod
03-22-2009, 11:44 PM
Hi hunsnowboard,

The best way to handle this is to make your Userform update the bookmark by adding/deleting the sentence, as appropriate. To do that, you'd use code along the lines of:
Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
If Documents.Count > 0 then
If ActiveDocument.Bookmarks.Exists(BmkNm) Then
Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
ActiveDocument.Bookmarks.Add BmkNm, BmkRng
End if
End if
Set BmkRng = Nothing
End sub
or, if you're confident that the required document will be loaded with its bookmarks intact:

Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
ActiveDocument.Bookmarks.Add BmkNm, BmkRng
Set BmkRng = Nothing
End sub
where your Userform passes both the bookmark’s name and contents to the above macro.

Simply deleting the bookmarked text, along with the bookmark, gives you no way to redeem the situation if an error is made when updating the Userform. With the above approach, you don't even need to worry about having the bookmark pre-populated with the sentence in question.

geekgirlau
03-24-2009, 10:14 PM
You can do this quite simply with fields in the document. If the value of the bookmark is set in your code, in the document you can use something like this:


{ if { ref MyBookmark }<>"" "This is the sentence to appear if the bookmark is filled" "" }

macropod
03-24-2009, 10:44 PM
Hi geekgirlau,

Not in this case, since the 'MyBookmark' to which you refer is the bookmarked range. What you're suggesting only works for a cross-reference to the bookmark.

fumei
03-25-2009, 11:40 AM
That is, a location that is NOT the bookmark itself.

macropod: I really like that you mentioned possible corrections.

"Simply deleting the bookmarked text, along with the bookmark, gives you no way to redeem the situation if an error is made when updating the Userform. "

This is something that is often not thought about and is decidely worthy of mention. Handling "ooops...I made a mistake" is the not the same as "normal" error-handling.

Good one.

geekgirlau
03-25-2009, 04:51 PM
Actually macropod, although it's difficult to say without seeing the doucment in question, people use bookmarks in a couple of different ways: to "mark" a location, or as a storage repository for text (a faux variable if you like).

If the purpose is the latter, where the bookmark sits is irrelevant. Using fields as I have suggested means that the bookmark is not deleted even if you make an error, because you are not typing anything in the bookmark location.

Personally I find bookmarks tend to be problematic if you are working with lots of them. I like to use the custom document properties, which can be read without having to open the document if necessary, and using fields where appropriate to display text.

macropod
03-25-2009, 05:36 PM
Hi geekgirlau,

Somehow I'm not sure you've understood the problem.

Hunsnowboard's document has a range of bookmarked text. Under certain conditions, the sentence containing that bookmark has to be deleted. As posted, your solution won't work because it doesn't address that issue.

What your solution does is to use a REF field to test the contents of a bookmark in order to determine what should be displayed somewhere else. What you've proposed would only work if the bookmark being tested was inside the true result of the IF test, but there's no indication that's what you had in mind.

What I've proposed is, in effect, that the whole sentence be bookmarked and that the code in the userform be used to show that sentence (along with whatever is already meant to be inserted into the bookmark) or to delete the sentence.

The suitability a custom document property (in conjunction with a DOCPROPERTY field) depends on whether any of the text concerned has anything other than the default setting - it couldn't bold a word or add superscripting to an ordinal number, for example.

As for the creative use of bookmarks and fields generally, I think I've got that covered - see:
http://www.wopr.com/index.php?showtopic=249902&st=0&p=249902&#entry249902 (http://www.wopr.com/index.php?showtopic=249902&st=0&p=249902&#entry249902)
http://www.wopr.com/index.php?showtopic=365442&st=0&p=365442&#entry365442 (http://www.wopr.com/index.php?showtopic=365442&st=0&p=365442&#entry365442)
http://www.wopr.com/index.php?showtopic=670027&st=0&p=670027&#entry670027 (http://www.wopr.com/index.php?showtopic=670027&st=0&p=670027&#entry670027)
http://www.wopr.com/index.php?showtopic=731107&st=0&p=731107&#entry731107 (mhtml:{2603C627-B617-484B-B3E3-521BCFB808E8}mid://00000569/!x-usc:http://www.wopr.com/index.php?showtopic=731107&st=0&p=731107&#entry731107)

geekgirlau
03-25-2009, 07:25 PM
No, I understood the problem. I'm merely pointing out an alternative method of dealing with it. Your method is correct, but not the only solution.

The REF field can be modified easily to show whatever text you like for the true and/or the false value of the IF statement. The advantage is that it's transparent. When users are modifying bookmarked ranges directly this is not the case, and I've seen multiple instances where people have attempted to modify bookmarked ranges and managed to get themselves into difficulty.

macropod
03-25-2009, 07:39 PM
Hi geekgirlau,

OK, but the IF test won't 'fire' automatically either. You'd have to update the field (eg using ActiveDocument.Fields. Update). And you'd have to do that every time the field on the userform is updated, since the IF test needs to be activated every time the bookmark's text is changed in order for the change to be reflected in the document.

hunsnowboard
03-26-2009, 12:32 PM
Thank you Everyone for your replies! Got it working! Thank you again!