Monday, March 31, 2008

Check Existence of BookMark using VBA (Word VBA)

Find BookMarks using VBA

BookMarks are vital in Word. However, when you look for a particular bookmark using VBA, it will cease to exist causing 5101 - This bookmark does not exist error.

To avoid this it is better to use Exists Method to check if the Bookmark exist.

Sub Check_If_BookMark_Exists()

If ActiveDocument.Bookmarks.Exists("TempBKMK") = True Then
ActiveDocument.Bookmarks("TempBKMK").Range.Text = "Something"
End If

End Sub


Exists method determines whether the specified bookmark or task exists. Returns True if the bookmark or task exists

5 comments:

  1. Anonymous9:29 AM

    Thanks a lot! Just what I was looking for, and it worked like a charm.

    ReplyDelete
  2. Anonymous11:28 AM

    Thank you so much. I needed this due to newly corrupted bookmarks and had been futile-ly trying things for hours till I found this. You have helped me immensely.
    John D.

    ReplyDelete
  3. Anonymous5:23 AM

    I have extend this code with some other snippets to check for broken Bookmarks and URLs.
    Here the code, some people might finf it useful:

    Regards
    Henrik

    Option Explicit

    Private Declare Function IsValidURL Lib "urlmon" (ByVal pBC As Long, _
    url As Byte, ByVal dwReserved As Long) As Long
    Sub x()
    '
    ' x Macro
    ' Macro recorded 7/19/2007 by David Chinell
    '
    Dim objWorkRange As Range
    Dim objField As Field
    Dim aryCode() As String
    Dim strFileName As String
    Dim AllLinksValid As Boolean


    ' Set this to handle bad file formats

    On Error Resume Next

    ' Work from the current insertion point down

    Set objWorkRange = ActiveDocument.Range
    objWorkRange.Start = Selection.Start

    ' Find all the hyperlinks
    AllLinksValid = True
    For Each objField In objWorkRange.Fields
    ' For each hyperlink, split out the filename
    If objField.Type = wdFieldHyperlink Then

    aryCode = Split(objField.Code, """")
    strFileName = aryCode(1)
    ' Test whether the file exists

    If ActiveDocument.Bookmarks.Exists(strFileName) <> True Then
    If Not ValidURL(strFileName) Then
    ' The file is missing
    objField.Select
    AllLinksValid = False
    Exit For
    End If
    End If
    End If
    Next objField

    Set objWorkRange = Nothing

    If AllLinksValid Then
    MsgBox _
    Prompt:="All links and bookmarks are valid!" & vbNewLine, _
    Buttons:=vbInformation, _
    Title:="LazyButt Tools"
    Else: _
    MsgBox Prompt:="This link is invalid:" & _
    vbNewLine & strFileName _
    , Buttons:=vbInformation _
    , Title:="LazyButt Tools"
    End If

    End Sub


    Function ValidURL(ByVal url As String) As Boolean

    Dim b() As Byte

    ' We need this because we're passing a Unicode string
    b = url & vbNullChar

    If IsValidURL(0, b(0), 0) = 0 Then
    ValidURL = True ' valid URL
    Else
    ValidURL = False ' invalid URL
    End If

    End Function

    ReplyDelete
  4. Actually I was looking for exactly this - to check for broken links using VBA. Thanks to the 'Anonymous' who posted it

    ReplyDelete
  5. Anonymous6:44 AM

    Wow, what a valuable code, thanks Henrik!

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.