Miscellaneous tasks

This topic includes Visual Basic examples for the following tasks:

Changing the view

The View object includes properties and methods related to view attributes (show all, field shading, table gridlines, and so on) for a window or pane. The following example changes the view to print view.

ActiveDocument.ActiveWindow.View.Type = wdPrintView

Setting text in a header or footer

The HeaderFooter object is returned by the Headers, Footers and HeaderFooter properties. The following example changes the text of current page header.

ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Range.Text = "Header text"

This example creates a Range object (oRange) that references the primary footer for the first section in the active document. After the Range object is set, the existing footer text is deleted. The FILENAME field is added to the footer along with two tabs and the AUTHOR field.

Set oRange = ActiveDocument.Sections(1) _
    .Footers(wdHeaderFooterPrimary).Range
With oRange
    .Delete
    .Fields.Add Range:=oRange, Type:=wdFieldFileName, Text:="\p"
    .InsertAfter Text:=vbTab & vbTab
    .Collapse Direction:=wdCollapseStart
    .Fields.Add Range:=oRange, Type:=wdFieldAuthor
End With

Setting options

The Options object includes properties that correspond to items in the Options dialog box (Tools menu). The following example sets three application options for Word.

With Options
    .AllowDragAndDrop = True
    .ConfirmConversions = False
    .MeasurementUnit = wdPoints
End With

Changing the document layout

The PageSetup contains all the page setup attributes of a document (left margin, bottom margin, paper size, and so on) as properties. The following example sets the margin values for the active document.

With ActiveDocument.PageSetup
    .LeftMargin = InchesToPoints(0.75)
    .RightMargin = InchesToPoints(0.75)
    .TopMargin = InchesToPoints(1.5)
    .BottomMargin = InchesToPoints(1)
End With

Looping through paragraphs in a document

This example loops through all of the paragraphs in the active document. If the space before setting for a paragraph is 6 points, this example changes the spacing to 12 points.

For Each aPara In ActiveDocument.Paragraphs
   If aPara.SpaceBefore = 6 Then oPara.SpaceBefore = 12
Next aPara

For more information, see Looping through a collection.

Customizing menus and toolbars

The CommandBar object represents both menus and toolbars. Use the CommandBars property with a menu or toolbar name to return a single CommandBar object. The Controls property returns a CommandBarControls object that refers to the items on the specified command bar. The following example adds the Word Count command to the Tools menu.

CustomizationContext = NormalTemplate
CommandBars("Tools").Controls.Add _
    Type:=msoControlButton, ID:=792, Before:=6

The following example adds the Double Underline command to the Formatting toolbar.

CustomizationContext = NormalTemplate
CommandBars("Formatting").Controls.Add _
    Type:=msoControlButton, ID:=60, Before:=7

Turn on the macro recorder and customize a menu or toolbar to determine the ID value for a particular command (for example, ID 60 is the Double Underline command).