填写表格 ms-word VBA / 最有效的 Word 智能导航
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11988327/
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
Filling out a form ms-word VBA / Most effective intelligent navigation through Word
提问by Mike Kellogg
Summary:My company sends out letters to customers and wants me to write a macro to fill in different text form fields that have been set up in a word document since a very limited amount of things change between different letters sent out. I've done macros in excel, but this is my first encounter with ms-word macros
摘要:我的公司向客户发送信件,并希望我编写一个宏来填写已在 Word 文档中设置的不同文本表单字段,因为发送的不同信件之间的变化非常有限。我在excel中做过宏,但这是我第一次遇到ms-word宏
Problem:I've had a hard time finding effective, intelligent navigation through ms-word. I did find this article about moving in different directionswhich is really the same as the arrow keys. Was hoping to get some insight into the best way to navigate the word document. For example should I do a loop of moving to the right 1 space until I find something meaningful or is there a more effective way?
问题:我很难通过 ms-word 找到有效的智能导航。我确实找到了这篇关于向不同方向移动的文章, 这与箭头键实际上相同。希望深入了解导航word文档的最佳方式。例如,我应该循环移动到正确的 1 空间直到找到有意义的东西还是有更有效的方法?
Question:Is it possible to store a specific form location in a variable or does ms-word have no coordinate system?
问题:是否可以将特定的表单位置存储在变量中或 ms-word 没有坐标系?
Thanks in advance!
提前致谢!
采纳答案by Mike Kellogg
There are at least three types of Form Field in recent Windows version of Word - Content Controls, "Legacy Form Fields" and ActiveX Form Fields. Assuming you are dealing with Legacy Form Fields, you should be able to index the FormFields collection using the bookmark name, then use, e.g.
在最近的 Windows 版本的 Word 中,至少有三种类型的表单域 - 内容控件、“传统表单域”和 ActiveX 表单域。假设您正在处理旧表单字段,您应该能够使用书签名称索引 FormFields 集合,然后使用,例如
ActiveDocument.FormFields("Text1").Result = "mytextformfieldresult"
ActiveDocument.FormFields("DropDown1").Result = "mydropdownformfieldresult"
ActiveDocument.FormFields("Check1").Checkbox.Value = True
回答by Siddharth Rout
To work with the content controls, you have to ensure that you have set the Title
of the control. You can also set the Tag
as well if you want. See Snapshot
要使用内容控件,您必须确保已设置Title
控件的 。Tag
如果需要,您也可以设置。查看快照
And then you can use this code to update the content control or retrieve it's value
然后您可以使用此代码更新内容控件或检索其值
Sub Sample()
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "MyTextBox1" Then
cc.Range.Text = "Hello World!"
Exit For
End If
Next cc
End Sub
When you run the code, this is how it looks
当你运行代码时,它是这样的
To get the control's text you can use Debug.Print cc.Range.text
要获取控件的文本,您可以使用 Debug.Print cc.Range.text
回答by dsolimano
You should be able to create bookmarks at various places in the document that you need to go to. In modern Word, this is on the "Insert" tab, in the "Links" group. Then you can access the bookmarks from VBA and insert text into them with this sort of code:
您应该能够在文档中需要访问的不同位置创建书签。在现代 Word 中,它位于“插入”选项卡上的“链接”组中。然后,您可以从 VBA 访问书签并使用以下代码将文本插入其中:
ActiveDocument.Bookmarks("myBookmark").Range.InsertBefore "Inserted Text"
This MVP sitehas more data on the technique. Also, MSDNhas some data on bookmark objects.