Each Word document contains at least one section by default and can contain multiple sections as needed for its contents and layout. The section of the document controls the page layout so that different sections of a document can use different page layouts if necessary.
You can add a section to a document either by using the Add method with the Sections collection or by using the InsertBreak method with a Range or Selection object.
The Add method takes the following syntax:
expression.Add Range, Start
Here, expression is a required expression that returns a Sections collection. Range is an optional Variant argument specifying the range at the beginning of which to insert the break. (If you omit Range, VBA inserts the break at the end of the document.) Start is an optional Variant argument used to specify the type of section break to insert:
wdSectionContinuous (0) for a continuous break
wdSectionEvenPage (3) for an even-page break
wdSectionOddPage (4) for an odd-page break
wdSectionNewColumn (1) for a new-column break
wdSectionNewPage (2, the default) for a new-page break
The following statement adds a new-page section to the active document, placing it before the second paragraph:
ActiveDocument.Sections.Add _ Range:=.Range(Start:=.Paragraphs(2).Range.Start, _ End:=.Paragraphs(2).Range.Start), Start:=wdSectionNewPage
The InsertBreak method takes the following syntax:
expression.InsertBreak Type
Here, expression is a required expression that returns a Selection or Range object. Type is an optional Variant argument specifying the type of section break to be inserted:
wdSectionBreakNextPage (2) for a new-page break
wdSectionBreakContinuous (3) for a continuous break
wdSectionBreakEvenPage (4) for an even-page break
wdSectionBreakOddPage (5) for an odd-page break
wdColumnBreak (8) for a new-column break
The following statement inserts a continuous section break before the second paragraph in the active document:
ActiveDocument.Paragraphs(2).Range.InsertBreak _ Type:=wdSectionBreakContinuous
To change the page setup of a document or a section, you work with the PageSetup object of the application Document object or Section object. For example, the following statements work with the PageSetup object of the document named Planning.docm, setting letter-size paper, portrait orientation, mirror margins, and margin measurements (in points):
With Documents("Planning.docm").PageSetup .PaperSize = wdPaperLetter
.Orientation = wdOrientPortrait .TopMargin = 1 .BottomMargin = 1 .LeftMargin = 1 .RightMargin = 1.5 .MirrorMargins = True End With
To open a new window containing an open document, use the Add method. Its syntax is straightforward:
expression.Add window
Here, expression is an expression that returns a Windows collection, and window is an optional Variant argument specifying the window containing the document for which you want to open a new window. If you omit window, VBA opens a new window for the active document.
For example, the following statements open a new window for the first window open for the active document, assigning the window to the variable myWindow:
Dim myWindow As Window Set myWindow = Windows.Add(Window:=ActiveDocument.Windows(1))
Occasionally, it's useful to open one or more new windows for a document. If you do so, sooner or later you'll need to close all the secondary windows to give yourself more room to maneuver. The following statements close all windows for the active document except the first window:
Dim myWin As Window, myDoc As String myDoc = ActiveDocument.Name For Each myWin In Windows If myWin.Document = myDoc Then _ If myWin.WindowNumber <> 1 Then myWin.Close Next myWin
To split a window in two parts horizontally, set its Split property to True. To specify the split percentage (which controls how far down the window, measuring vertically, the split is placed), set the SplitVertical property. The following statements split the active window 70 percent of the way down the window:
With ActiveWindow .Split = True .SplitVertical = 70 End With
To remove the split from the window, set the Split property to False:
ActiveWindow.Split = False
To display the Document Map for a window at the Document Map's previous width percentage (of the document's window), set the DocumentMap property to True:
ActiveWindow.DocumentMap = True
To display the Document Map at a different width, or to change the width of the Document Map, set the DocumentMapPercentWidth property to a suitable percentage of the window's width:
ActiveWindow.DocumentMapPercentWidth = 25
To hide the Document Map again, set the DocumentMap property to False or set the DocumentMapPercentWidth property to 0.
To scroll a window up, down, left, or right, use either the LargeScroll method or the SmallScroll method.
The LargeScroll method is analogous to clicking before or after the thumb (the scroll box) on the horizontal or vertical scroll bar of a window; it scrolls the contents of the window by one "screen" (or multiple screens) in the specified direction. The SmallScroll method is analogous to clicking the scroll buttons on a scroll bar (the arrows at the top and bottom of the scroll bar); it scrolls the contents of the window up or down by one line and left or right by a small scroll increment.
The syntax for the LargeScroll method is as follows:
expression.LargeScroll(Down, Up, ToRight, ToLeft)
The syntax for the SmallScroll method is almost identical:
expression.SmallScroll(Down, Up, ToRight, ToLeft)
Here, expression is a required expression that returns a Window object. Down, Up, ToRight, and ToLeft are optional Variant arguments that specify the number of screens (for LargeScroll) or lines or horizontal movement units (for SmallScroll) to scroll the contents of the window in the directions their names indicate.
The following statement scrolls the active window up two screens:
ActiveWindow.LargeScroll Up:=2
To arrange a number of windows, use the Arrange method. The syntax for the Arrange method is as follows:
expression.Arrange ArrangeStyle
Here, expression is an expression that returns a Windows collection, and ArrangeStyle is an optional Variant argument that specifies how to arrange the windows: as icons (wdIcons, 1) or tiled (wdTiled, 0). The default is wdTiled.
For example, the following statement tiles the open windows:
Windows.Arrange ArrangeStyle:=wdTiled
To position a window, set its Left and Top properties. For example:
ActiveWindow.Left = 100 ActiveWindow.Top = 200
To size a window, set its Height and Width properties:
With ActiveWindow .Height = 300 .Width = 400 End With
To maximize, minimize, or "restore" a window, set its WindowState property to wdWindowStateMaximize, wdWindowStateMinimize, or wdWindowStateNormal, respectively. The following statements maximize the window containing the document named Example.docm if the window is minimized:
With Documents("Example.docm").Windows(1) If .WindowState = wdWindowStateMinimize Then _ .WindowState = wdWindowStateMaximize End With
After opening or arranging windows, you'll often need to make sure that an item you want the user to see — a range, some text, a graphic or other shape, or a field — is displayed in the window. The easiest way to do so is to use the ScrollIntoView method of the Window object. This method moves the view but not the selection, so if you need the selection to move to the range, you'll need to move it there separately.
The ScrollIntoView method takes the following syntax:
expression.ScrollIntoView(Obj, Start)
Here, expression is a required expression that returns a Window object. Obj is a required argument specifying a Range or Shape object. Start is an optional Boolean argument that you can set to True (the default) to have the upper-left corner of the range or shape displayed or False to have the lower-right corner displayed. Specify False for Start when you need to make sure the end of a range or shape that may be larger than the window is displayed.
The following statements position the selection at the end of the last paragraph in the first list in the active document, ready to add a new paragraph to the list:
Dim rngFirstList As Range Set rngFirstList = ActiveDocument.Lists(1).Range ActiveDocument.Windows(1).ScrollIntoView Obj:=rngFirstList, Start:=False rngFirstList.Select Selection.Collapse Direction:=wdCollapseEnd Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdMove
To change a document's view, set the Type property of the View object for the appropriate window to wdMasterView, wdNormalView, wdOutlineView, wdPrintPreview, wdPrintView, wdReadingView, or wdWebView. For example, the following statement changes the view for Sample.docm to Print Layout view:
Documents("Sample.docm").Windows(1).View.Type = wdPrintView
To zoom Print Layout view or Print Preview to display multiple pages, set the PageColumns and PageRows properties of the appropriate View object. (Change the view first if necessary.) The following statement displays Sample.docm in Print Layout view with six pages displayed (three across by two deep):
With Documents("Sample.docm").Windows(1).View .Type = wdPrintView With .Zoom .PageColumns = 3 .PageRows = 2 End With End With
18.118.128.226