How to use macro in Word document?

Macro to delete bookmarks in word file.

I am using invoke code (vbnet):

'Add a reference to the Microsoft.Office.Interop.Word namespace in UiPath Studio

'Create an instance of the Word application
Dim objWord As New Microsoft.Office.Interop.Word.Application

'Open the document
Dim objDoc As Microsoft.Office.Interop.Word.Document = objWord.Documents.Open(“path\filname.docx”)

'Delete all bookmarks that start with an underscore
For Each bkm As Microsoft.Office.Interop.Word.Bookmark In objDoc.Bookmarks
If bkm.Name Like “_*” Then
bkm.Delete()
End If
Next

'Save and close the document
objDoc.Save()
objDoc.Close()

'Quit the Word application
objWord.Quit()

'Release the objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(objDoc)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord)
objDoc = Nothing
objWord = Nothing

  1. The bookmarks are not deleted.
  2. Tried running macros in excel, no compilation error, still bookmarks are not deleted.

Unable to understand what mistake am I making in the code. If you know any other method apart from the above please let me.

@Matt_Davis

I guess you need to use bokm.Text instead of bkm.Name…

Name will give the name of book mark…if need to check the text is startign with underscore use text instead

Hope this helps

Cheers

Bookmarks that start with an underscore are hidden. You need to show them first before looping through the bookmarks.

Add this before your For Each-loop:

objDoc.Bookmarks.ShowHidden = true

image

image

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.