Batch substitution of labels in Word with VBA implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.