Bookmarks are a great way to specify important locations on the word document. Using VBA you can move the cursor to those bookmarks.
Creating Bookmarks Manually:
For example assume you have the following data in a word document:
We want to be able to modify the value for each field using VBA for Word. Using bookmarks this process can be greatly simplified.
Step 1: Move the cursor before the name field:
Step 2: Create a bookmark:
Step 3: Do the same for the other fields:
VBA and Bookmarks:
Now that we have created bookmarks for the different fields in the last section we can easily modify each field. For example lets say we want to modify the name field to john. We could use the code below:
Sub main() Selection.GoTo What:=wdGoToBookmark, Name:="bmName" Selection.EndKey Unit:=wdLine, Extend:=wdExtend Selection.Text = " John" & vbCr End Sub
The line below moves the cursor to the bookmark named “bmName”: