Export (0) Print
Expand All

Using New Document Class Properties in Word 2010

Office 2010

Office Quick Note banner

Handy Programming Tips for Microsoft Word 2010: Learn how to use some of the new properties of the Document class in Microsoft Word 2010.

Applies to:    Microsoft 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:

Add the Code to the Visual Basic Editor

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

  1. Start Word 2010.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    NoteNote:

    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.

  3. In the Projects pane, click ThisDocument.

  4. 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
    
    

Test the Solution

In this task, you run the VBA code that manipulates various properties of the Document class.

To run the code

  1. In the document, type the following command (without the quotation marks). This adds one paragraph of five sentences to the document.

    “=rand(1,5)”

  2. Drag the Visual Basic Editor window to the right side of your monitor.

  3. Drag the Word window to the left side of your monitor and adjust the windows until you can see them both.

  4. Press F8 to step through the code line-by-line and watch the behavior as the code progresses.

Next Steps

Community Additions

ADD
Show:
© 2014 Microsoft