Filling out the ms-word VBA form / The most effective intelligent navigation through Word

Summary: My company sends out letters to customers and wants me to write a macro to fill in the different fields of the text form that were set in the text document, since a very limited number of things change between different sent letters. I made macros in excel, but this is my first encounter with ms-word macros

Problem: It was difficult for me to find effective smart navigation through ms-word. I found this article about moving in different directions which is actually the same as the arrow keys. I was hoping to get an idea of โ€‹โ€‹the best way to navigate through a text document. For example, should I make the right cycle 1 until I find something meaningful or a more efficient way?

Question: Is it possible to save the specific location of the form in a variable or not ms-word without a coordinate system?

Thanks in advance!

+8
vba ms-word word-vba
source share
3 answers

In the latest version of Windows Word - Content Controls, "Legacy Form Fields" and Field Form Fields there are at least three types of form fields. Assuming you are dealing with obsolete form fields, you should be able to index the FormFields collection using the bookmark name, and then use, for example.

ActiveDocument.FormFields("Text1").Result = "mytextformfieldresult" ActiveDocument.FormFields("DropDown1").Result = "mydropdownformfieldresult" ActiveDocument.FormFields("Check1").Checkbox.Value = True 
+6
source share

To work with content controls, you need to make sure that you have set the Title of the control. You can also install Tag if you want. See Snapshot

enter image description here

And then you can use this code to update the content control or get its 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, it looks like this:

enter image description here

To get control text, you can use Debug.Print cc.Range.text

+7
source share

You should be able to create bookmarks in different 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 paste the text with the following code into them:

 ActiveDocument.Bookmarks("myBookmark").Range.InsertBefore "Inserted Text" 

This MVP site contains more data about the method. In addition, MSDN contains some data about bookmark objects.

+2
source share

All Articles