21.3. Working with Sections, Page Setup, Windows, and Views

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.

21.3.1. Adding a Section to a Document

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

21.3.2. Changing the Page Setup

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

21.3.3. Opening a New Window Containing an Open Document

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.

Understanding the Two Windows Collections

There are two Windows collections: one for the application and one for the windows displaying the document with which you're working. The Windows collection for the Document object can be useful if you have multiple windows open for the same document (as you can do by clicking the Ribbon's View tab, then clicking the New Window button in the Window section of the Ribbon), but usually you'll want to use the Windows collection for the Application object. Windows is a creatable object, so you don't need to specify the Application object.


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))

21.3.4. Closing All Windows but the First for a Document

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

21.3.5. Splitting a Window

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

21.3.6. Displaying the Document Map for a Window

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.

21.3.7. Scrolling a Window

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

21.3.8. Arranging Windows

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

21.3.9. Positioning and Sizing a Window

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

21.3.10. Making Sure an Item Is Displayed in the Window

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

21.3.11. Changing a Document's View

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

21.3.12. Zooming the View to Display Multiple Pages

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.128.226