Doc variables in Word template are not being updated from Excel VBA

430 Views Asked by At

I have certain ranges within an Excel file and I create an instance of Word template from within Excel VBA. This Word template has a bunch of Doc variables so that I can replace the values of those Doc Variables with my defined variables using precise Excel sheet ranges. For some weird reason, Doc variable laid down in Word template file are not being updated.

Could someone please extend your help and advise what possible mistake I might be making here.

Sub Generate_CoverLetter()

    Dim client, bo, invoice_currency, vesselName As String
    Dim invoiceAmount 'As Single
    Dim bo_rng, rngBO, rngCustName, rngAmount, rngVesselName, rngCurrencyCode, rngCommencedDate As Range
    Dim SearchParams As Variant
    Dim SearchParamsCols As Variant
    Dim wdApp As Object
    Dim wdDoc As Word.Document
    
    Set bo_rng = Application.InputBox( _
      Title:="Select BO range", _
      Prompt:="Select a cell to pull in BO number....", _
      Type:=8)
      
    bo = bo_rng.value
    
    lastRow = Range("A" & Application.Rows.Count).End(xlUp).Row

    SearchParams = Array("field_19", "customer_name", "total_amount", "invoice_currency_code", "field_43", "field_71")
    orderColumn = seachParamColumn(SearchParams(0))
    cnameColumn = seachParamColumn(SearchParams(1))
    amountColumn = seachParamColumn(SearchParams(2))
    currencyColumn = seachParamColumn(SearchParams(3))
    commencedDateColumn = seachParamColumn(SearchParams(4))
    vesselColumn = seachParamColumn(SearchParams(5))
    
    
    ' core ranges
    Set rngBO = Range(Cells(2, orderColumn), Cells(lastRow, orderColumn))
    Set rngCustName = Range(Cells(2, cnameColumn), Cells(lastRow, cnameColumn))
    Set rngAmount = Range(Cells(2, amountColumn), Cells(lastRow, amountColumn))
    Set rngVesselName = Range(Cells(2, vesselColumn), Cells(lastRow, vesselColumn))
    
    ' invoice parameter ranges
    Set rngCurrencyCode = Cells(bo_rng.Row, currencyColumn)
    Set rngCommencedDate = Cells(bo_rng.Row, commencedDateColumn)


    client = Cells(bo_rng.Row, rngCustName.Column)
    InvoiceNumber = Cells(bo_rng.Row, 1) ' .value
    invoice_currency = rngCurrencyCode.value
    invoiceAmount = Application.WorksheetFunction.SumIfs(rngAmount, rngBO, "=" & bo, rngCustName, "=" & client)
    invoiceAmount = Format(invoiceAmount, "#,##0.00")
    commencedDate = rngCommencedDate.value

    ' This weird looking loop is here because an order might have several rows in Excel data
    ' but only one of those rows might have the name of the vessel

    For Each cell In rngVesselName.SpecialCells(xlCellTypeVisible)
        If cell.Column = vesselColumn And Not IsEmpty(cell) Then
            vesselName = cell.value
            Exit For
        End If
    Next
    
    
    MsgBox InvoiceNumber & vbLf & _
            invoice_currency & " " & invoiceAmount & vbLf & _
            client & vbLf & _
            vesselName & vbLf & _
            commencedDate

Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add

Set wdDoc = wdApp.Documents.Open("C:\Users\smiq\Documents\Custom Office Templates\CL.dotm")
Set wdDoc = ActiveDocument
wdDoc.Activate

wdApp.WindowState = wdWindowStateMaximize

ActiveDocument.Variables("wd_vesselName").value = vesselName
ActiveDocument.Variables("wd_CommencedDate").value = commencedDate
ActiveDocument.Variables("wd_invoiceNumber").value = InvoiceNumber
ActiveDocument.Variables("wd_invoiceAmount").value = invoiceAmount

End Sub


Function seachParamColumn(param As Variant)
Dim c
    With Range("1:1")
    Set c = .Find(param, , xlValues)
        If Not c Is Nothing Then
         seachParamColumn = Range(c.Address).Column
        End If
    End With

End Function

0

There are 0 best solutions below