Delete bookmarks using userform (code is not working as expected)

nmc

New Member
Joined
Aug 25, 2022
Messages
38
Office Version
  1. 2021
Platform
  1. Windows
Hi, I writed a code that should delete the bookmarks in a wordfile selected by the user. I run the macro but in the end the bookmarks are not being deleted.

VBA Code:
Option Explicit

Dim wdApp As Object
Dim wdDoc As Object
Dim wdAppCreated As Boolean

Private Sub Add_Click()
    Dim selectedItem As String
    
    ' Check if an item is selected in ListBox1
    If Me.ListBox1.ListIndex <> -1 Then
        selectedItem = Me.ListBox1.List(Me.ListBox1.ListIndex)
        
        ' Add the selected item to ListBox2
        Me.ListBox2.AddItem selectedItem
        
        ' Remove the selected item from ListBox1
        Me.ListBox1.RemoveItem Me.ListBox1.ListIndex
    End If
End Sub

Private Sub Undo_Click()
    Dim selectedItem As String
    
    ' Check if an item is selected in ListBox2
    If Me.ListBox2.ListIndex <> -1 Then
        selectedItem = Me.ListBox2.List(Me.ListBox2.ListIndex)
        
        ' Add the selected item to ListBox1
        Me.ListBox1.AddItem selectedItem
        
        ' Remove the selected item from ListBox2
        Me.ListBox2.RemoveItem Me.ListBox2.ListIndex
    End If
End Sub

Private Sub Cancel_Click()
    'Close the Userform
    Unload Me
End Sub

Private Sub Finish_Click()
    Dim bookmarkName As String
    Dim bm As Object
    Dim i As Long
    
    On Error GoTo ErrorHandler

    ' Loop through each item in ListBox1 and replace the empty spaces with underscores in the bookmark name
For i = 0 To Me.ListBox1.ListCount - 1
    bookmarkName = Replace(Me.ListBox1.List(i), " ", "_")
    If wdDoc.Bookmarks.Exists(bookmarkName) Then
        MsgBox "Deleting bookmark: " & bookmarkName ' display the name of the bookmark being deleted
        wdDoc.Bookmarks(bookmarkName).Delete
    End If
Next i

    On Error GoTo 0

    ' Save and close the Word document
    wdDoc.Save
    wdDoc.Close
    
    ' Close the Word application
    wdApp.Quit
    
    ' Close the Userform
    Unload Me

    Call ReplaceWordsBetweenAngleBrackets
    
    Exit Sub
    
ErrorHandler:
    MsgBox "An error has occurred: " & Err.Description
End Sub

Private Sub UserForm_Initialize()
    Dim bm As Object
    
    On Error GoTo ErrorHandler
    
    ' Try to get an existing instance of Word, or create a new instance if none is found
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo ErrorHandler
    
    ' Open the specified Word document
    Set wdDoc = wdApp.Documents.Open("C:\Temp.print\wordTemplate.docx")
    
    ' Populate the ListBox with the bookmarks
    For Each bm In wdDoc.Bookmarks
        If Left(bm.Name, 4) <> "logo" And Left(bm.Name, 6) <> "footer" Then
            Me.ListBox1.AddItem Replace(bm.Name, "_", " ")
        End If
    Next bm
    
     
    ' Set a flag indicating whether the Word application was created in this routine
    wdAppCreated = (Err.Number <> 0)
    
    Exit Sub
    
ErrorHandler:
    MsgBox "An error has occurred: " & Err.Description
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you temporarily comment out or delete On Error GoTo ErrorHandler from Finish_Click(), and then run it, do you get an error? If so, which one, and on which line?
 
Upvote 0
Is not appearing any error.
In the end when I open the final file the bookmark still there
 
Upvote 0
When U add the bookmarks to the listbox, you're changing their names...
Code:
' Populate the ListBox with the bookmarks
    For Each bm In wdDoc.Bookmarks
        If Left(bm.Name, 4) <> "logo" And Left(bm.Name, 6) <> "footer" Then
            Me.ListBox1.AddItem Replace(bm.Name, "_", " ")
        End If
    Next bm
You're not changing the name of your bookmarks back to the original bookmark name. This doesn't reverse it...
Code:
For i = 0 To Me.ListBox1.ListCount - 1
    bookmarkName = Replace(Me.ListBox1.List(i), " ", "_")
    If wdDoc.Bookmarks.Exists(bookmarkName) Then
        MsgBox "Deleting bookmark: " & bookmarkName ' display the name of the bookmark being deleted
        wdDoc.Bookmarks(bookmarkName).Delete
    End If
Next i
Seems like this would return the bookmark name to original...
Code:
bookmarkName = Replace(Me.ListBox1.List(i), "_", " ")
If this is correct you currently shouldn't be getting the deleting bookmark msgbox. HTH. Dave
 
Upvote 0
But when I Run the code deletes the bookmark but doesn't delete the text of the bookmark

Example:

If I have a bookmark named as Clause_1 with the text "Hello"

After running the code the name bookmark Will not appear any more but the text "Hello" appear in the word file.. it seems is only deleting the property of bookmark name
 
Upvote 0
In that case, try...

VBA Code:
wdDoc.Bookmarks(bookmarkName).Range.Text = ""

Hope this helps!
 
Upvote 0
Disregard my dyslexic post. You did have it right.. my bad. Domenic has it right. Have a nice day. Dave
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top