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!!

RE: Searching for a variable bookmark name by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Thu Jul 17 06:54:02 PDT 2008

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

"Capitalleaf" wrote:

> 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!!

Re: Searching for a variable bookmark name by Helmut

Helmut
Thu Jul 17 06:56:09 PDT 2008

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

Re: Searching for a variable bookmark name by Jay

Jay
Thu Jul 17 07:01:05 PDT 2008

Capitalleaf wrote:
> 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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



RE: Searching for a variable bookmark name by Capitalleaf

Capitalleaf
Thu Jul 17 07:06:04 PDT 2008

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!!

"StevenM" wrote:

> 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
>
> "Capitalleaf" wrote:
>
> > 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!!

RE: Searching for a variable bookmark name by Capitalleaf

Capitalleaf
Thu Jul 17 07:47:23 PDT 2008



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

> "StevenM" wrote:
>
> > 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
> >
> > "Capitalleaf" wrote:
> >
> > > 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!!

RE: Searching for a variable bookmark name by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Thu Jul 17 08:21:04 PDT 2008

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

RE: Searching for a variable bookmark name by Capitalleaf

Capitalleaf
Thu Jul 17 09:04:01 PDT 2008

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!

"StevenM" wrote:

> 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

RE: Searching for a variable bookmark name by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Thu Jul 17 10:33:02 PDT 2008

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


"Capitalleaf" wrote:

> 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!
>
> "StevenM" wrote:
>
> > 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

Re: Searching for a variable bookmark name by Jonathan

Jonathan
Wed Jul 23 04:01:15 PDT 2008


"Capitalleaf" <Capitalleaf@discussions.microsoft.com> wrote in message
news:BE6602CC-2806-434A-BCA3-28464C7D413C@microsoft.com...
>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

> Application.ScreenUpdating = True
> End Sub
>
>


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup