In your procedures, you'll often need to check the status of options in the Word application or in a particular document. In VBA, many of the options are controlled by the Options object, which has dozens of properties but no methods.
This section shows four brief examples of setting options, three examples using the Options object and one using a property of the Document object. To see the full list of properties available for the Options object, click the Options object in the object model map (see Figure 20.1).
Hyperlinks in Word documents have proved a mixed blessing — especially since Microsoft's changes to the way Word handles hyperlinks tend to leave users unsure whether to click or Ctrl+click the hyperlink to follow it. You can set the CtrlClickHyperlinkToOpen property of the Options object to True to ensure that hyperlinks require Ctrl+clicking:
Options.CtrlClickHyperlinkToOpen = True
Setting this option to False means you can trigger links by merely clicking them — no Ctrl key required.
To make sure your procedures behave as expected, you may need to check that Word is using Insert mode rather than Overtype mode. (In Insert mode, Word inserts the characters you type at the insertion point, moving any text to the right of the characters over, to make room. In Overtype mode, each character you type replaces the character to the right of the insertion point.)
Overtype mode is controlled by the Overtype property of the Options object. When OverType is True, Overtype mode is on; when Overtype is False, Insert mode is on. The following statements store the user's current Overtype setting in a Boolean variable named blnOvertypeOn, set Overtype to False, perform its actions, and then restore the user's Overtype setting:
Dim blnOvertypeOn As Boolean blnOvertypeOn = Options.Overtype Options.Overtype = False 'take actions here Options.Overtype = blnOvertypeOn
When configuring Word on a computer, you may need to make sure that its default file paths are set to the correct folders. You can do so by working with the DefaultFilePath property of the Options object. The syntax is as follows:
expression.DefaultFilePath(Path)
Here, expression is a required expression that returns an Options object. Often, it's easiest to use the Options object itself. Path is one of the self-explanatory enumerated constants shown in the following list:
For example, the following statements set the user templates path and the workgroup templates path:
Options.DefaultFilePath(wdUserTemplatesPath) = _ "c:users ichardappdata oamingmicrosoft emplates" Options.DefaultFilePath(wdWorkgroupTemplatesPath) = _ "\serverusers emplates"
Before running a procedure that adds, deletes, or formats text, you may need to turn off the Track Changes feature so that the changes the procedure makes are not marked. If the user had Track Changes on, you should turn it back on at the end of the procedure so that changes the user makes are tracked again. Remember that it's usually a good practice when changing options to store the user's current setting in a variable, carry out your procedure's task, and then restore the user's original setting.
The following example saves the user's setting for the TrackRevisions option in the ActiveDocument object in a Boolean variable named blnTrackChangesOn, sets TrackRevisions to False, performs its actions, and then restores the user's TrackRevisions setting:
Dim blnTrackChangesOn As Boolean blnTrackChangesOn = ActiveDocument.TrackRevisions ActiveDocument.TrackRevisions = False 'take actions here ActiveDocument.TrackRevisions = blnTrackChangesOn
18.119.255.18