【问题标题】:What is a DOCVARIABLE in word什么是 Word 中的 DOCVARIABLE
【发布时间】:2019-07-23 16:59:06
【问题描述】:

什么是 Microsoft Word 2003 中的DOCVARIABLE?我该如何设置?如何让它显示在我的 Word 文档中?

【问题讨论】:

    标签: ms-word docvariable


    【解决方案1】:

    您可以使用 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
    

    【讨论】:

    • 但是这段代码在哪里?是 Word 脚本还是什么?
    • 如果该方法运行多次,该方法将导致抛出错误。 VBA(在 Word 2013 中测试)将允许您使用例如 ActiveDocument.Variables("FieldName").Value = fieldValue 即时创建/设置值
    【解决方案2】:

    如何让它显示在我的word文档中:

    Insert->Field->Category:DocumentAutomation->Field Names:DocVariable->Field COdes Button->然后输入变量名。

    【讨论】:

      【解决方案3】:

      我也在寻找这个问题的答案。 创建了一个小脚本来显示所有 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 2011-07-26
        • 1970-01-01
        • 2017-10-31
        • 2010-12-20
        • 1970-01-01
        • 2020-05-18
        相关资源
        最近更新 更多