我正在尝试为Xero构建模板发票。 Xero会在 MS Word 模板中查找特定字段,并以给定格式输入分配给该文本字段名称的变量。在单词中,您可以切换字段代码以仅查看字段名称:
{ MERGEFIELD InvoiceNumber \* MERGEFORMAT}
或格式为的名称:
INV1234
这会将Sub InvoiceNumber()
Dim MyInv As FormFields
Set MyInv = ActiveDocument.FormFields
If MyInv("Text1").Result = "InvoiceNumber" Then
MyInv("Text1").CheckBox.Value = Right(MyInv("Text1"), 4)
End If
End Sub
成功输出到模板中。现在我需要做的就是输出最后4个字符。
This post似乎暗示必须使用VBA。我将宏与 Visual Basic 放在一起,这就是我遇到麻烦的地方:
{{1}}
返回
错误5941:所选择的成员不存在
我是一个初学者,在单词中使用VB宏,我做错了什么,我应该如何尝试调用InvoiceNumber字段?
答案 0 :(得分:2)
请尝试使用以下解决方案:
Sub InvoiceNumber()
Dim MyInv As Field
Set MyInv = GetFieldByName("InvoiceNumber")
If Not MyInv Is Nothing Then
'do something with field result...
'here... debug to Immediate window
Debug.Print Right(MyInv.Result, 4)
End If
End Sub
Function GetFieldByName(fName As String) As Field
Dim F As Field
For Each F In ActiveDocument.Fields
'if not working try with (1) istead of (2) in line below
If Split(Replace(F.Code, " ", " "), " ")(2) = fName Then
Set GetFieldByName = F
Exit Function
End If
Next F
Set GetFieldByName = Nothing
End Function