【发布时间】:2019-07-23 16:59:06
【问题描述】:
什么是 Microsoft Word 2003 中的DOCVARIABLE?我该如何设置?如何让它显示在我的 Word 文档中?
【问题讨论】:
标签: ms-word docvariable
什么是 Microsoft Word 2003 中的DOCVARIABLE?我该如何设置?如何让它显示在我的 Word 文档中?
【问题讨论】:
标签: ms-word docvariable
您可以使用 Microsoft Visual Basic for Applications 变量集合来设置和检索 Word 文档或模板中字符串变量的内容。
此外,您可以使用 DocVariable 字段来检索已设置为在 Word 文档中显示的文档变量的值。
来源:How to store and retrieve variables in Word documents
Sub GetSetDocVars()
Dim fName As String
fName = "Jeff Smith"
' Set contents of variable "fName" in a document using a document
' variable called "FullName".
ActiveDocument.Variables.Add Name:="FullName", Value:=fName
' Retrieve the contents of the document variable.
MsgBox ActiveDocument.Variables("FullName").Value
End Sub
【讨论】:
ActiveDocument.Variables("FieldName").Value = fieldValue 即时创建/设置值
如何让它显示在我的word文档中:
Insert->Field->Category:DocumentAutomation->Field Names:DocVariable->Field COdes Button->然后输入变量名。
【讨论】:
我也在寻找这个问题的答案。 创建了一个小脚本来显示所有 ActiveDocument.Variables
这里是:
Sub GetVariables()
' Declaration of output variavle, which is a string
Dim output As String
output = ""
For Each Variable In ActiveDocument.Variables
' & is used for string concatenation.
output = output & Variable.Name & " = " & Variable & vbNewLine
Next
MsgBox output
End Sub
【讨论】: