我在 Excel 文件中有某些范围,我从 Excel VBA 中创建了一个 Word 模板的实例。这个 Word 模板有一堆 Doc 变量,因此我可以使用精确的 Excel 工作表范围将这些 Doc 变量的值替换为我定义的变量。由于某些奇怪的原因,Word 模板文件中的 Doc 变量没有被更新。
有人可以提供您的帮助并建议我在这里可能犯的错误。
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