PDA

View Full Version : Solved: to delete what starts with...



wax_village
10-12-2008, 03:09 PM
Hi everybody!

Thanks for your great efforts here!

I am new to VBA (although not for programming!!!) and I will highly appreciate your help.

As for the code:


ActiveDocument.Bookmarks("BookmarkName").Delete


specifying the BookmarkName, it is ok.

But what I want to do is to delete bookmarks starting with specific set of characters, e.g. "aaa*".

I have tried several things, but nothing worked well. Could you please help me with this?

Thanks in advance,
Mina

Demosthine
10-12-2008, 07:47 PM
Good Evening.

Try:


Dim bmkCurrent as Bookmark

For Each bmkCurrent in ActiveDocument.Bookmarks
If Left(bmkCurrent.Name, 3) = "aaa" Then
bmkCurrent.Delete
End If
Next bmkCurrent


Scott

lucas
10-12-2008, 07:50 PM
Hi Mina,
This works for me in Word 2003

I attached a file too for you to check it out with. Run the sub delBkmks


Option Explicit
Sub delBkmks()
Dim objBookmark As Bookmark
For Each objBookmark In ActiveDocument.Bookmarks()
If objBookmark.Name <> _
Right(objBookmark.Name, Len("aaa") - 3) Then
objBookmark.Range.Delete
End If
Next
End Sub



Note: This method also deletes the range of the bookmark. You didn't specify but it's probably relevant. Easy to alter if that's not what you need.

lucas
10-12-2008, 07:52 PM
I'm 3 minutes late and used the opposite approach.....it's good to have options.

Demosthine
10-12-2008, 07:57 PM
Hey Lucas.

You're absolutely right, it's good to have options.

The one difference I would like to note; however, is that your code deletes the Bookmark plus the text that's within that Bookmark's Range. Mine deletes only the Bookmark. That's a key difference depending on what Mina needs accomplished.

Thanks for helping on the Forum.
Scott

lucas
10-12-2008, 08:04 PM
Hi Scott, I had already thought of that and I added a line to my post to point that out. Sometimes it's hard to tell what folks want if they aren't specific.

Thanks for your contributions here also Scott, folks like you are what help make this a great place to exchange ideas.

Welcome to the forum.

wax_village
10-13-2008, 12:20 AM
Dear Scott and Lucas,

First of all, I like the wonderful spirit you show.

Acutally, I need to delete bookmarks only. So Scott's approach works well for me.

Many thanks Scott and Lucas,
Mina

lucas
10-14-2008, 03:59 PM
Hi Mina, glad you got the help you needed. Be sure to mark your thread solved using the thread tools at the top of the page.