Re: if bookmark.Exist (Left("nut", 3) by macropod
macropod
Sun Apr 06 23:26:11 PDT 2008
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" <rz@cu.com> wrote in message news:OEw%23u5GmIHA.3780@TK2MSFTNGP06.phx.gbl...
> 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
>
>