if bookmark.Exist (Left("nut", 3)

R

Rue

Hi all

I am hoping to use VBA to check if a document has a bookmark starting with a
particular string. I thought I could use LEFT to check if it exists but it
does not pick up the bookmark.
Checking if bookmark exits works perfectly when not using LEFT

Ive tried:
If ActiveDocument.Bookmarks.Exists(Left("_BD", 3)) = True Then
msgbox "Yes its here"
end if

I found that looping through the document works but I do want to search for
each bookmark eg:

Dim myBM As Bookmark

For Each myBM In ActiveDocument.Bookmarks
If ActiveDocument.Bookmarks.Exists(myBM.Name) And Left(myBM.Name, 3) =
"_BD" then
do this
End If
Next myBM

Any ideas?

Thanks in advance
 
M

macropod

Hi Rue,

With your first code snippet's If test (ie If ActiveDocument.Bookmarks.Exists(Left("_BD", 3)) = True), what you're effectively
testing is:
If ActiveDocument.Bookmarks.Exists("_BD") = True)
In other word's your code is simply looking for a bookmark named _BD.

With your second code snippet, you don't need to test whether the bookmark exists, since 'Each myBM In ActiveDocument.Bookmarks'
means the loop can only be working with the known set of bookmarks. Thus your code could be simplified to:

Dim myBM As Bookmark
For Each myBM In ActiveDocument.Bookmarks
If Left(myBM.Name, 3) = "_BD" then
'do this
End If
Next myBM

Cheers
 
R

Rue

Bewty many thanks :)


macropod said:
Hi Rue,

With your first code snippet's If test (ie If
ActiveDocument.Bookmarks.Exists(Left("_BD", 3)) = True), what you're
effectively testing is:
If ActiveDocument.Bookmarks.Exists("_BD") = True)
In other word's your code is simply looking for a bookmark named _BD.

With your second code snippet, you don't need to test whether the bookmark
exists, since 'Each myBM In ActiveDocument.Bookmarks' means the loop can
only be working with the known set of bookmarks. Thus your code could be
simplified to:

Dim myBM As Bookmark
For Each myBM In ActiveDocument.Bookmarks
If Left(myBM.Name, 3) = "_BD" then
'do this
End If
Next myBM

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Rue said:
Hi all

I am hoping to use VBA to check if a document has a bookmark starting
with a particular string. I thought I could use LEFT to check if it
exists but it does not pick up the bookmark.
Checking if bookmark exits works perfectly when not using LEFT

Ive tried:
If ActiveDocument.Bookmarks.Exists(Left("_BD", 3)) = True Then
msgbox "Yes its here"
end if

I found that looping through the document works but I do want to search
for each bookmark eg:

Dim myBM As Bookmark

For Each myBM In ActiveDocument.Bookmarks
If ActiveDocument.Bookmarks.Exists(myBM.Name) And Left(myBM.Name, 3) =
"_BD" then
do this
End If
Next myBM

Any ideas?

Thanks in advance
 

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