The best way to select a form field using VBA

Article contributed by Mark Tangard

If you want to select a form field using a macro, you can use a variety of methods; but only the least obvious method is bug-free!

The line:




will indeed move the cursor to the formfield called Text4, but if that formfield is empty, it won't become highlighted, as a formfield reached by ordinary tabbing will, and this looks very odd to the user.

The line:

Selection.GoTo What:=wdGoToBookmark, Name:="Text4"

will move the cursor to Text4 and highlight the field, but in certain cases (depending on the vertical position of the fields – and this is especially noticeable if the formfields are in a table), that motion will cause the screen to jump in a very annoying way, even if the destination field is visible on the screen to start with. 

Fortunately, the line:


will move the cursor to Text4 and highlight the entire field. The screen will not jump unless the Text4 field is not fully visible to begin with. Convoluted, but it works!

Related articles

How to set the tab order of a Word form

How to validate the contents of a formfield in a Word form