简体   繁体   中英

How do I add an UpdateAllFields for my Word doc, to my VBA Macros in Excel?

I have an Excel macros that pulls data from my workbook, opens a Word template as new, and updates some bookmarks we have.

We then Select All, and click Update All to populate the rest of the document with the information using cross references.

Instead of manually performing the "Update All Fields", I'd like to just add it to my Excel macros so that after it updates the bookmarks, it happens automatically.

Here is a small version of the code currently:

Private Sub CreateTemplate1(tPath As String, r As Integer)
Dim wdApp As Object
Dim wdDoc As Object

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then Set wdApp = CreateObject("Word.Application")
On Error GoTo 0

wdApp.Visible = True
'wdApp.DisplayAlerts = False
Set wdDoc = wdApp.Documents.Open(FileName:=tPath)

With wdDoc
    Dim obj_BMRange     As Object

    Set obj_BMRange = wdDoc.Bookmarks("STPNumber").Range
    WriteToBookmarkRetainBookmark obj_BMRange, Range("L1").Value

    Set obj_BMRange = wdDoc.Bookmarks("SiteAddress").Range
    WriteToBookmarkRetainBookmark obj_BMRange, Range("C" & r).Value
End With
End Sub

Function WriteToBookmarkRetainBookmark(rng As Object, content As String)
Dim sBkmName As String

sBkmName = rng.Bookmarks(1).Name
rng.Text = content
rng.Document.Bookmarks.Add sBkmName, rng
End Function

Please let me know where to add the Update All Fields code.

This will update all the fields in wdDoc

    Dim field As field
    For Each field In wdDoc.Fields
        field.Update
    Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM