Recently in the project, to achieve the automatic generation of contracts, and then want to use VBA for the contract (Word version) in the same place in the bulk of the replacement.
The field in the Word document is replaced by a label. The code is as follows:
Public Function Updatebookmarkloop (app as Object, bookmarkbase as String, nvalue as Variant, bot as long, top as long) Dim counter As Long Dim name as String counter=1For counter=bot to top name= Bookmarkbase &Counter Updatebookmark app, name, Nvalue ' here bot and top refers to the entire label in Word. From the first few to the first few. Then the entire label name is made up of labels and the number of loops in the back
' Then replace the label by calling the following Updatebookmark method. Next counterEnd functionpublic Function updatebookmark (app as Object, BookmarkName as String, newvalue as Variant)If app. ActiveDocument.Bookmarks.Exists (bookmarkname) Thenapp. ActiveDocument.Bookmarks (BookmarkName). SelectApp. Selection.Text =NewValueIf NewValue =""Then
' Debug.Print ' Deleting bookmark "
' App. ActiveDocument.Bookmarks (BookmarkName). Delete
' Debug.Print ' "Bookmark deleted"
End If Else ' Debug.Print BookmarkName & ' does not exist ' End If /c7>end Function
A simple method call enables the operation of bulk label substitutions, as follows:
Text.updatebookmarkloop Wdapp, "Manager a name", Managernamea, 1, 14
Replace the label labeled Manage Person a name. There are a total of 14. Loop to replace.
At this point, the batch labels in Word are replaced.
Batch substitution of labels in Word with VBA implementation