Searching for a variable bookmark name

C

Capitalleaf

I have multiple bookmarks in a document that all begin with "ABC...". The
"..." can be any combination of letters and numbers at varying lenghts. Is
there a way to search for all bookmarks within the document that begin with
"ABC" using VBA? Once found, the bookmark will be deleted. I'm using Word
2007. Please let me know if more info is needed. Thank you!!
 
S

StevenM

To: Capitalleaf

Sub DeleteABCBookmarks()
Dim i As Long
Dim actDoc As Document
Set actDoc = ActiveDocument
For i = actDoc.Bookmarks.Count To 1 Step -1
If Left(actDoc.Bookmarks(i).Name, 3) = "ABC" Then
actDoc.Bookmarks(i).Delete
End If
Next i
End Sub

Steven Craig Miller
 
H

Helmut Weber

Hi,

Sub test777()
Dim oBkm As Bookmark
For Each oBkm In ActiveDocument.Bookmarks
If Left(oBkm.name, 3) = "ABC" Then
oBkm.Delete
End If
Next
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

Capitalleaf said:
I have multiple bookmarks in a document that all begin with "ABC...".
The "..." can be any combination of letters and numbers at varying
lenghts. Is there a way to search for all bookmarks within the
document that begin with "ABC" using VBA? Once found, the bookmark
will be deleted. I'm using Word 2007. Please let me know if more
info is needed. Thank you!!

You need to iterate through the whole collection, testing each bookmark to
see whether its name matches your criterion.

Here's sample code:

Sub demo()
Dim bk As Bookmark

For Each bk In ActiveDocument.Bookmarks
With bk
If Len(.Name) > 3 Then
If UCase(Left(.Name, 3)) = "ABC" Then
.Range.Delete
End If
End If
End With
Next
End Sub

If you meant that you wanted to remove the bookmark but leave the text
that's inside it, then change .Range.Delete to just .Delete.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
C

Capitalleaf

Thanks for the reply Steven!

Can you tweek the code slightly for another step? Once the bookmark is
found (which is contained in a table) I would like to select the entire row
that contains the bookmark and delete that.

Thanks again!!
 
C

Capitalleaf

I was able to figure out how to get the row to delete and the code works
great. Can this same concept be applied when searching for text in a
document?


Thank you,

Lenny
 
S

StevenM

To: Capitalleaf

Re: Can this same concept be applied when searching for text in a document?

It all depends on what you're searching for, how you'll find it, and what
you want to do with it once you've found it. I'm sure if you gave us a few
more details, you'll recieve three or more answers. <grin>

Steven Craig Miller
 
C

Capitalleaf

I actually got the macro to work with what I was trying to do. The only
question I have is if there is a better way for the following code to be
written (to run more efficiently with both aspects (or loops), deleting the
rows with bookmarks and deleting the rows with certain text). It really
works fine, but being as new as I am, that's something I know I don't know.
If you have any suggestions, they would certainly be appreciated.

Sub Delete_ABC()
Application.ScreenUpdating = False
Selection.ExtendMode = True
Dim i As Long
Dim actDoc As Document
Set actDoc = ActiveDocument
For i = actDoc.Bookmarks.Count To 1 Step -1
If Left(actDoc.Bookmarks(i).Name, 3) = "ABC" Then
actDoc.Bookmarks(i).Range.Rows.Select
Selection.SelectRow
Selection.Rows.Delete
End If
Next i

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Words"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.SelectRow
Selection.Rows.Delete
Loop

Selection.ExtendMode = False
Application.ScreenUpdating = True
End Sub


Thank you!
 
S

StevenM

To: Capitalleaf,
actDoc.Bookmarks(i).Range.Rows.Select
Selection.SelectRow
Selection.Rows.Delete

Could read: actDoc.Bookmarks(i).Range.Rows.Delete

And
Selection.SelectRow
Selection.Rows.Delete

Could read: Selection.Rows.Delete


Steven Craig Miller
 
J

Jonathan West

Capitalleaf said:
I actually got the macro to work with what I was trying to do. The only
question I have is if there is a better way for the following code to be
written (to run more efficiently with both aspects (or loops), deleting
the
rows with bookmarks and deleting the rows with certain text). It really
works fine, but being as new as I am, that's something I know I don't
know.
If you have any suggestions, they would certainly be appreciated.

Sub Delete_ABC()
Application.ScreenUpdating = False
Selection.ExtendMode = True
Dim i As Long
Dim actDoc As Document
Set actDoc = ActiveDocument
For i = actDoc.Bookmarks.Count To 1 Step -1
If Left(actDoc.Bookmarks(i).Name, 3) = "ABC" Then
actDoc.Bookmarks(i).Range.Rows.Select
Selection.SelectRow
Selection.Rows.Delete

The three lines above can be replaced with a single line

actDoc.Bookmarks(i).Range.Rows.Delete
End If
Next i

If processing a whole document, it is generally better to avoid using the
selection - it causes the cursor to jump about and slows down execution.
Instead, use a Range object variable.

Change this
Selection.Find.ClearFormatting
With Selection.Find

to this

Dim myRange as Range
Set myRange = actDoc.Content
With myRange.Find
.Text = "Words"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

and change this
End With
Do While Selection.Find.Execute
Selection.SelectRow
Selection.Rows.Delete
Loop

Selection.ExtendMode = False

to this

Do While .Execute
myRange.Rows.Delete
Loop
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top