Some time ago, I created this macro with vba which worked very well with Office 2003. It still works with Office 2021 as long as you don't modify the Word file. If I change it, it can no longer see the bookmark to replace. Microsoft Office Mysteries. It is important to underline that I have to replace several (100 or more) bookmarks.
An example:
Sub MA01()
'
' MA01 Macro
' Macro registrata il 18/12/2015 da Esterni
'
Sheets("Lineare").Select 'go to a specific excel sheet where are the imputs
' get all the imputs from the cell (true or false value) to check or uncheck a specific formfield
Cross01 = Range("C669").Value ' get first value to check or uncheck a specific formfield after
Cross02 = Range("C668").Value ' get second value to check or uncheck a specific formfield after
'continue and so on
' get all the imputs from the cell to replace after a specific bookmark
Stringa001 = Range("C650").Value 'get first value to replace after a specific bookmark
Stringa002 = Range("C28").Value 'get second value to replace after a specific bookmark
'continue and so on
' open a specific file word to make the change after
Const sFILENAME As String = "C:\test\test.doc" ' file name here
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(sFILENAME)
With wrdDoc
' check or uncheck a specific formfield
.FormFields("Controllo01").CheckBox.Value = Cross01
.FormFields("Controllo02").CheckBox.Value = Cross02
'continue and so on
' replace a specific Bookmark
.Bookmarks("X001").Range.Text = Stringa001
.Bookmarks("X002").Range.Text = Stringa002
'continue and so on
' go to a spefific sheet of excel and into a specific cell
Sheets("Ins Dati").Select
Range("Q68").Select
End With
MsgBox ("OK !!!! Document created")
End Sub
I discovered that the bookmark I inserted was not recognized by VBA in newer versions. I used legacy bookmarks but for an unknown reason they were not seen, by VBA, in recent versions of Office.
To ensure that the bookmark is recognized in the most recent versions of Office you need to insert the bookmark from the drop-down menu INSERT - Bookmarks (ICON) and then add bookmark, THAT bookmark is seen by VBA, while the others inserted via drop-down menu development legacy module (icon) do not work.
Big BUG for me in the new versions of office. (I spent a lot of time trying to figure out what the hell happened) Thanks MS for complicate my life.