vba 从 Word 文档表单控件获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19690833/
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
Getting data from Word doc form controls
提问by jasongetsdown
I'm building a form in a Word .docm (macro enabled Word 2013 doc) with the aim of programming an Access database to import data from completed forms. I've placed textBox and comboBox controls to receive user input, but I can't get the data back out.
我正在 Word .docm(启用宏的 Word 2013 文档)中构建一个表单,目的是对 Access 数据库进行编程以从已完成的表单中导入数据。我已经放置了 textBox 和 comboBox 控件来接收用户输入,但是我无法取回数据。
The examples I've seen use the Document.FormFields
collection like so (in Word):
我见过的例子Document.FormFields
像这样使用集合(在 Word 中):
Dim fld as FormField
for each fld in ActiveDocument.FormFields
Debug.Print fld.Name & " - " & fld.Result.Text
next
However in my doc Document.FormFields
is empty, but Document.Fields
has 19 elements, which happens to be the number of controls in my form. That's great, except that I can't seem to get the name or value of any of the controls using a Field
object. Field.Result.Text
is always blank, and there is no Field.Name
attribute.
但是在我的文档中Document.FormFields
是空的,但Document.Fields
有 19 个元素,这恰好是我表单中控件的数量。太好了,除了我似乎无法使用Field
对象获取任何控件的名称或值。Field.Result.Text
始终为空白,并且没有Field.Name
属性。
So what's the difference between Field
objects and FormField
objects, and why are my controls showing up in Fields
when all the examples I've seen use FormFields
?
那么Field
对象和FormField
对象之间有什么区别,为什么我Fields
看到的所有示例都使用了我的控件FormFields
?
Am I using the wrong form controls? There are three types (I hope I'm not the only one who thinks this is ridiculous) legacy controls, ActiveX controls, and content controls. I'm using the ActiveX type.
我是否使用了错误的表单控件?存在三种类型(我希望我不是唯一认为这很荒谬的人)遗留控件、ActiveX 控件和内容控件。我正在使用 ActiveX 类型。
采纳答案by Chris Rolliston
A few things...
一些东西...
In MS Word terms, a 'field' doesn't have to be a form field. E.g., an auto-updated date, linked graphic, page number, etc., are all types of 'field' (or at least, were until the most recent versions of Word).
For compatibility reasons, it can be better to avoid the ActiveX controls. E.g., the Mac version of Word doesn't support them.
For best compatibility I would personally stick to the traditional form controls. Instances are named according to their bookmark name, which is settable by right clicking the control and selecting Properties. In VBA, their data is then got via the FormFields collection; if you want the value for a specific field, use
Value = ActiveDocument.FormFields("MyFieldName").Result
在 MS Word 术语中,“字段”不必是表单字段。例如,自动更新的日期、链接的图形、页码等都是“字段”类型(或者至少在 Word 的最新版本之前)。
出于兼容性原因,最好避免使用 ActiveX 控件。例如,Mac 版本的 Word 不支持它们。
为了获得最佳兼容性,我个人会坚持使用传统的表单控件。实例根据它们的书签名称命名,可以通过右键单击控件并选择属性来设置。在 VBA 中,它们的数据是通过 FormFields 集合获得的;如果您想要特定字段的值,请使用
Value = ActiveDocument.FormFields("MyFieldName").Result
回答by Blackhawk
If all you want is to use the current fields you have, you can get the value or name from the OLEFormat.Object:
如果您只想使用您拥有的当前字段,则可以从 OLEFormat.Object 获取值或名称:
Application.ActiveDocument.Fields.Item(1).OLEFormat.Object.Value
Or
或者
Application.ActiveDocument.Fields.Item(1).OLEFormat.Object.Name
However, I would agree with Chris in recommending that you avoid the ActiveX controls. As Microsoft says, there are a lot of reasons that they aren't the best choice for Word forms, except in very specific cases.
但是,我同意 Chris 建议您避免使用 ActiveX 控件。正如微软所说,有很多原因表明它们不是 Word 表单的最佳选择,除非在非常特殊的情况下。
回答by Ryan The Leach
Using the "legacy" formfields and
使用“遗留”表单域和
//key
Application.ActiveDocument.FormFields(1).Range.Fields(1).Code
//value
Application.ActiveDocument.FormFields(1).Result
Is the solution I came up with.
是我想出的解决方案。
I've seen companies use the bookmarks, default values, status / help texts for different purposes as well. Not that I would recommend it, but maybe they are working around an issue I'm not aware of.
我已经看到公司出于不同目的使用书签、默认值、状态/帮助文本。并不是说我会推荐它,但也许他们正在解决我不知道的问题。
I feel like .range is a bit of a hack, but considering it's the range of only the form field, unless form fields or fields can have nested fields, it should be ok.
我觉得 .range 有点hack,但考虑到它只是表单字段的范围,除非表单字段或字段可以有嵌套字段,否则应该没问题。