vba 如何在 MS WORD 2007 中编辑表单字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12598605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:51:54  来源:igfitidea点击:

How do i edit form field value in MS WORD 2007

vbams-wordword-vba

提问by hendrix

How do i edit Text Form Field (legacy forms) in my document, so that i can then use new value of this field in my VBA script?

如何编辑文档中的文本表单字段(旧表单),以便我可以在 VBA 脚本中使用该字段的新值?

I have simple form with Text Form Field (Bookmark = TextFormField1). It is showing default text "default text".

我有带有文本表单字段的简单表单(书签 = TextFormField1)。它显示默认文本“默认文本”。

I can access text of this field like this: ActiveDocument.FormFields("TextFormField1").Range.Text

我可以像这样访问这个字段的文本: ActiveDocument.FormFields("TextFormField1").Range.Text

However, if i change value of this field, it seems to delete the field itself, and replacing it with simple text - causing ActiveDocument.FormFields("TextFormField1").Range.Textto throw exception, because TextFormField1 does not exist anymore...

但是,如果我更改此字段的值,它似乎会删除该字段本身,并用简单的文本替换它 - 导致 ActiveDocument.FormFields("TextFormField1").Range.Text抛出异常,因为 TextFormField1 不再存在......

What am i doing wrong? How can i insert text in form field, and use this text in my VBA script?

我究竟做错了什么?如何在表单字段中插入文本,并在我的 VBA 脚本中使用此文本?

采纳答案by Olle Sj?gren

Use the Resultproperty:

使用Result属性:

ActiveDocument.FormFields("TextFormField1").Result = "Form field text"

回答by Вадим Онищенко

Try next code:

尝试下一个代码:

ThisDocument.FormFields("Index_Or_FormFieldName_or_Bookmarkname").TextInput.EditType Type:=wdRegularText, Default:="Your Form field text"  

But it rec the value in to the FormField very slowly.

但是它非常缓慢地将值记录到 FormField 中。