vba 构建 Word 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building Word fields
提问by JonnyGold
Apart from just inserting and parsing text into a blank Word field, is there any way to programmatically build user-defined fields and field codes into my own templates with VBA? Furthermore, is there a way to make these fields show up in the list of available fields?
除了将文本插入和解析到空白 Word 字段之外,还有什么方法可以使用 VBA 以编程方式将用户定义的字段和字段代码构建到我自己的模板中?此外,有没有办法让这些字段显示在可用字段列表中?
回答by Carl G
I recently developed a solution that used Word's MACROBUTTON and ADDIN field types.
我最近开发了一个使用 Word 的 MACROBUTTON 和 ADDIN 字段类型的解决方案。
I found MACROBUTTON useful because the third whitespace-delimited entry inside the field (programmatically field.code.text) is displayed within Word. This allows my users to watch fields as they move around. { MACROBUTTON NoMacro * } would display an "*" in Word, e.g. And it would do nothing when the user double-clicked on it, because I have purposefully not defined a macro named "NoMacro".
我发现 MACROBUTTON 很有用,因为字段内的第三个空格分隔条目(以编程方式 field.code.text)显示在 Word 中。这允许我的用户在他们四处走动时观察字段。{ MACROBUTTON NoMacro * } 会在 Word 中显示一个“*”,例如当用户双击它时它什么也不做,因为我故意没有定义一个名为“NoMacro”的宏。
The ADDIN field does not display (except when display field codes is turned on) and stores a hidden string in its field.data property. Using this field I could have a hidden field the contents of which could not be seen or modified by users (excepting that if they turn on "show field codes" they can see that it is an ADDIN field (but they cannot see/edit the "data" property), and that they can delete this field just like any other field.)
ADDIN 字段不显示(除非打开显示字段代码)并在其 field.data 属性中存储隐藏字符串。使用这个字段,我可以有一个隐藏字段,其内容不能被用户看到或修改(除非他们打开“显示字段代码”,他们可以看到它是一个 ADDIN 字段(但他们不能看到/编辑“数据”属性),并且他们可以像删除任何其他字段一样删除此字段。)
I found these pages useful:
我发现这些页面很有用:
回答by Fionnuala
What had you in mind? It is possible to add custom document properties either manually or with VBA. These are the accessible as fields under DOCPROPERTY:
你想到了什么?可以手动或使用 VBA 添加自定义文档属性。这些是 DOCPROPERTY 下可访问的字段:
{ DOCPROPERTY "Test" \* MERGEFORMAT }
You can use a macro to ensure that the custom property is added to documents:
您可以使用宏来确保将自定义属性添加到文档中:
Sub AutoNew()
Dim objCustomProperties As DocumentProperties
Set objCustomProperties = ActiveDocument.CustomDocumentProperties
objCustomProperties.Add Name:="Test", _
Type:=msoPropertyTypeString, Value:="Blah", _
LinkToContent:=False
End Sub
Further Information
更多信息
Automacros: http://msdn.microsoft.com/en-us/library/aa263747(office.10).aspx
自动宏:http: //msdn.microsoft.com/en-us/library/aa263747(office.10) .aspx
Understanding Custom Document Properties in Microsoft Office Word 2003: http://msdn.microsoft.com/en-us/library/aa537154.aspx
了解 Microsoft Office Word 2003 中的自定义文档属性:http: //msdn.microsoft.com/en-us/library/aa537154.aspx