Using New Document Class Properties in Word 2010
Handy Programming Tips for Microsoft Word 2010: Learn how to use some of the new properties of the Document class in Microsoft Word 2010.
Last modified: May 10, 2011
Applies to: Office 2010 | VBA | Word 2010
Published: May 2011
Provided by: Frank Rice, Microsoft Corporation
The Document class represents a Microsoft Word document. In this topic, you programmatically work with some of the new properties of that class. To complete this task, you must do the following:
In this task, you add programming code that manipulates some of the new properties of the Document class.
To add code to the Visual Basic Editor
Start Word 2010.
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.
In the Projects pane, click ThisDocument.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Sub DocumentInformationProperties() Dim isFinal As Boolean Dim doLockQuickStyleSet As Boolean Dim doLockTheme As Boolean Dim doTrackFormatting As Boolean Dim doTrackRevisions As Boolean Dim doTrackMoves As Boolean With ActiveDocument ' Store away the current state: isFinal = .Final doLockQuickStyleSet = .LockQuickStyleSet doLockTheme = .LockTheme doTrackFormatting = .TrackFormatting doTrackRevisions = .TrackRevisions doTrackMoves = .TrackMoves ' This had better return true! Debug.Print .HasVBProject ' Work with Quick Styles .LockQuickStyleSet = False ' Now, on the ribbon, go to the Styles ' group, and select Change Styles. Select ' Style Set, and note the list of available quick styles. .LockQuickStyleSet = True ' Repeat the same steps, and now note that the ' menu no longer contains a list of available quick styles. ' Work with the Theme .LockTheme = False ' On the menu, click the Page Layout menu item. ' In the Themes group, note that the Themes drop-down is available. ' You can change the document's theme. .LockTheme = True ' Go to the same location, and note that now the Themes drop-down ' is no longer available. .TrackRevisions = True .TrackFormatting = True ' In the document, change a word to be bold. Note that the ' document tracks formatting changes. .TrackFormatting = False ' Change another word to be bold, and note that the document ' no longer tracks formatting changes. .TrackMoves = True ' In the document, move a word. Note that the ' document tracks moves. .TrackMoves = False ' Move another word, and note that the document ' no longer tracks moves. ' Indicate that the current document is final. ' This forces a save of the document if it's not ' already saved: .Final = True ' Note the banner that appears in the document. ' Click Edit Anyway to continue editing. ' Put things back the way they were: .Final = isFinal .LockQuickStyleSet = doLockQuickStyleSet .LockTheme = doLockTheme .TrackFormatting = doTrackFormatting .TrackRevisions = doTrackRevisions .TrackMoves = doTrackMoves ' Retrieve the contents as an XML stream, using the ' Open XML file format. Dim results As String results = .WordOpenXML MsgBox results End With End Sub
In this task, you run the VBA code that manipulates various properties of the Document class.
To run the code
In the document, type the following command (without the quotation marks). This adds one paragraph of five sentences to the document.
Drag the Visual Basic Editor window to the right side of your monitor.
Drag the Word window to the left side of your monitor and adjust the windows until you can see them both.
Press F8 to step through the code line-by-line and watch the behavior as the code progresses.