PDA

View Full Version : Removing multiple booksmarks from one combobox selection



cfbrian
06-13-2006, 09:21 PM
Hi
I have a document that has a few topics of related information, grouped in pasages spread over the entire text (similar information but in different wording). If I want to use a certain topic, I would have to go through the entire text to delete the unrelated passages. It is quite tedious!!

I have created a combobox with 5 items, relating to the 5 topics. My intention is, when I pick one of the selection, click 'OK' the code will go through the document and delete all the text that does not pertain to the wanted selection.

I have placed enclosed books throughout the document. As far as I know, even with similar topics, I have to name them different e.g. Jupiter_1, Jupiter_2. (I am a newbie, please advise if I can do it differently.)

Also, how do I write the code to delete all the bookmarks without having to spell out the different bookmarks repeatedly :

With ActiveDocument
If ComboBoxApplication.Value = "Mars" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Jupiter_1"
Selection.Delete
End If
End With

If ComboBoxApplication.Value = "Mars" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Jupiter_2"
Selection.Delete
End If
End With

Thanks

fumei
06-14-2006, 03:07 AM
True. Separate distinct bookmarks 9a s they are Range objects) must be explicitly named. So.

A chunk of text bookmarked as Jupiter_1
A chunk of text bookmarked as Jupiter_2
A chunk of text bookmarked as Jupiter_3
A chunk of text bookmarked as Jupiter_4

You want to delete all the bookmarks relating to Jupiter.

Have your combobox pass the topic to delete to a Sub, like:Sub DeleteTopic(strBM As String)
Dim oBM As Word.Bookmark
For Each oBM in ActiveDocument.Bookmarks
If Left(oBM.Name, Len(strBM) = strBM Then
oBM.Range.Delete
End If
Next
End Sub

You pass the parameter to the Sub from the combobox, assuming that you will do this with a commandbutton.Sub CommandButton_Click()
Call DeleteTopic (Combobox1.Text)
End SubThe commandbutton sub calls the DeleteTopic Sub with the text of the combobox. Obviously you would have to have the combobox items match the bookmark names, as in:

Combobox items

Mars
Jupiter
Comets
Saturn

Bookmarks

Mars_1
Mars_2
Mars_3

Jupiter_1
Jupiter_2
Jupiter_3

etc.

The DeleteTopic sub takes the string parameter, loops through the bookmarks in the document, deleting all those with input_parameter as the starting characters.

Note: your code used the Selection to go to the bookmarks, select it, then delete it. No need. Using my code the bookmark range is simply deleted. This removes the bookmarks itself AND the text with it. There is no movement of the Selection. The bookmarks (and text) would simply disappear.

Oh, and please use the VBA tags with your posts. Thanks.

fumei
06-14-2006, 03:11 AM
Oh, and obviously you will have to fine tune the logic. I just noticed that your logic is reverse. If "Mars" is selected, then "Jupiter" is deleted. This is not a problem really. Just reverse the logic.If Left(oBM.Name, Len(strBM) <> strBM Then If the bookmark name is NOT the input parameter, then delete it.

cfbrian
06-15-2006, 06:55 PM
Thanks! I was trying to understand the sub. But after much trial & error : pray2: , I manage to get it working but still does not know how the Left & Len work in the contect of the code

fumei
06-15-2006, 07:49 PM
Sub DeleteTopic(strBM As String)
' strBM is the String of the bookmark
' you want to delete
' say string is Mars
Dim oBM As Word.Bookmark
For Each oBM In ActiveDocument.Bookmarks
' this runs through ALL bookmarks
' say current bookmark is Jupiter_1
' test against Left(oBM.Name) to count of the Length of
' the input string.
' The length is Mars (= 4 characters)
' Left(Jupiter_1, 4) = "Jupi"
' so if "Jupi" = "Mars then delete it..Nope.
' say current bookmark is Mars_2
' length of input string is 4
' Left(Mars_2, 4) = Mars - a match!
' so delete it
' say current bookmark is Mars_47
' Left(Mars_47, 4) = Mars - a match!
' so delete it
If Left(oBM.Name, Len(strBM) = strBM Then
oBM.Range.Delete
End If
Next
End Sub Look up Left or Right in Help.

cfbrian
06-16-2006, 07:09 PM
The explaination really helps. Thanks again, Fumei!!

fumei
06-16-2006, 08:20 PM
You're welcome.