MergeFields are not appearing updated

nmc

New Member
Joined
Aug 25, 2022
Messages
38
Office Version
  1. 2021
Platform
  1. Windows
Hello
I'm trying to create a VBA code in excel that can replace the mergefields that were converted into doc variables with the data from excel.
But everytime that I run the code and open the word nothing changes. Can someone help fixing my code?
Thanks in advance
VBA Code:
Sub ReplaceDocVariables()
    Dim oWord As Word.Application
    Set oWord = New Word.Application
    Dim oDoc As Word.Document
    Set oDoc = oWord.Documents.Open("C:\Temp.print\wordTemplatee.docx")
    Dim oField As Word.field
    Dim xlApp As Excel.Application
    Set xlApp = New Excel.Application
    Dim xlWb As Excel.Workbook
    Set xlWb = xlApp.Workbooks.Open("C:\Temp.print\values.xlsx")
    Dim xlWs As Excel.Worksheet
    Set xlWs = xlWb.Sheets(1)
    Dim LastRow As Long
    LastRow = xlWs.Cells(xlWs.Rows.count, "A").End(xlUp).Row
    Dim i As Long
    For Each oField In oDoc.Fields
        If oField.Type = wdFieldDocVariable Then
            For i = 1 To LastRow
                If oField.Code.Text = "DOCVARIABLE " & xlWs.Cells(i, "A").Value Then
                    oField.Result.Text = xlWs.Cells(i, "B").Value
                End If
            Next i
        End If
    Next
    oDoc.Save
    oWord.Quit
    xlWb.Close
    xlApp.Quit
End Sub


Sub UnlinkDocVariables()
    Dim oWord As Word.Application
    Set oWord = New Word.Application
    Dim oDoc As Word.Document
    Set oDoc = oWord.Documents.Open("C:\Temp.print\wordTemplatee.docx")
    Dim oField As Word.field
    For Each oField In oDoc.Fields
        If oField.Type = wdFieldDocVariable Then
            oField.Select
            oWord.Selection.Cut
        End If
    Next
    oDoc.Save
    oWord.Quit
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,156
Messages
6,123,339
Members
449,098
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top