24.4. WOrking with Windows and Views

To get the PowerPoint window into the state you want, you'll often need to work with the window and with the view. PowerPoint uses two types of windows:

  • Document windows are windows that contain documents (presentation files) rather than slide shows. VBA considers document windows to be DocumentWindow objects organized into the DocumentWindows collection but represents them with Window objects organized into the Windows collection. (You'll see how this works shortly.)

  • Slide show windows are windows that contain open slide shows. VBA uses SlideShowWindow objects and the SlideShowWindows collection to represent slide show windows.

This section shows you how to work with document windows. You'll learn how to work with slide show windows in "Setting Up and Running a Slide Show" in Chapter 25, "Working with Shapes and Running Slide Shows."

The Windows collection contains a Window object for every open window in the PowerPoint application. When you open a presentation while working interactively, PowerPoint opens a window so that you can see the presentation. When you open a presentation via VBA, you can set the WithWindow argument of the Add method to msoFalse to prevent PowerPoint from displaying a window for the presentation. In the user interface you can also open further windows as necessary — for example, by clicking the New Window button in the Window section of the Ribbon's View tab.

24.4.1. Working with the Active Window

PowerPoint uses the ActiveWindow object to represent the window that is active (the window that has the focus). Only one window is active at a time. The active window is always the first Window object in the Windows collection — Windows(1).

If no window is open at all, or all open windows are hidden, there is no active window, and using the ActiveWindow object causes VBA to return an error. To make sure that a window is open, check whether the Count property of the Windows collection is 0 — for example:

If Windows.Count = 0 Then MsgBox "There is no active window.", vbOkOnly +_
    vbExclamation, "No Window Is Open"

When you're working with presentations using VBA, you may sometimes find that the ActiveWindow object is a handy way to access a presentation, especially for a macro that the user runs after choosing the presentation, slide, or other object that they want to affect. In other cases, you may find that the ActivePresentation object is a more convenient way to access the presentation you need to work with, or you may prefer to access the presentation via the Presentations collection.

24.4.2. Opening a New Window on a Presentation

To open a new window, use the NewWindow method of the appropriate Window object. This method takes no arguments. For example, the following statement opens a new window showing the contents of the active window:

ActiveWindow.NewWindow

24.4.3. Closing a Window

To close a window, use the Close method with the appropriate Window object. In PowerPoint, the Close method takes no arguments.

Be Careful When Closing Windows Programmatically

If the window you're closing is the last window open for the presentation, PowerPoint simply closes the window without prompting the user to save any unsaved changes. For this reason, be careful when closing windows.


For example, you might close all windows but one on a presentation:

Do While ActivePresentation.Windows.Count > 1
    ActivePresentation.Windows(ActivePresentation.Windows.Count).Close
Loop

Alternatively, you might use the Save method to save a presentation before closing its last window, as in the next example. (More simply, you could use the Close method to close the presentation itself after saving it.)

With ActivePresentation
    If .Path = " " Then
        MsgBox "Please save this presentation.", vbOKOnly
    Else
        .Save
        For Each myWindow In Windows
            .Close
        Next myWindow
    End If
End With

24.4.4. Activating a Window

To activate a window or one of its panes, use the Activate method of the appropriate Window object. For example, the following statement activates the first window open on the presentation Benefits.pptm:

Presentations("Benefits.pptm").Windows(1).Activate

24.4.5. Arranging and Resizing Windows

To arrange windows, use the Arrange method with the appropriate Windows collection. The syntax is as follows:

expression.Arrange(ArrangeStyle)

Here, expression is a required expression that returns a Windows collection. ArrangeStyle is a required argument that specifies how to arrange the windows: ppArrangeCascade (cascade the windows in an overlapping arrangement that lets you see the title bar of each window but the contents of only the front window) or ppArrangeTiled (tile the windows; the default setting).

You can maximize, minimize, or restore the application window by setting the WindowState property of the Application object to ppWindowMaximized, ppWindowMinimized, or ppWindowNormal. Similarly, within the application window, you can maximize, minimize, or restore a document by setting its WindowState property.

When a window is in a "normal" state (ppWindowNormal, not maximized or minimized), you can position it by using the Top and Left properties to specify the position of the upper-left corner of the window and size it by setting its Height and Width properties.

The following example maximizes the application window and cascades the document windows within it:

Application.WindowState = ppWindowMaximized
Windows.Arrange ArrangeStyle:=ppArrangeCascade

24.4.6. Changing the View

To change the view in a window, set the ViewType property of the appropriate Window object to the appropriate constant: ppViewHandoutMaster, ppViewMasterThumbnails, ppViewNormal, ppViewNotesMaster, ppViewNotesPage, ppViewOutline, ppViewPrintPreview, ppViewSlide, ppViewSlideMaster, ppViewSlideSorter, ppViewThumbnails, or ppViewTitleMaster. For example, the following statement switches the active window into Slide Sorter view:

ActiveWindow.ViewType=ppViewSlideSorter

To zoom the view, specify a value from 10 to 400 for the Zoom property of the View object for the appropriate window. The value represents the zoom percentage, but you do not include the percent sign. For example, the following statement zooms the active window to 150 percent:

ActiveWindow.View.Zoom = 150

24.4.7. Working with Panes

The Pane object represents a pane of the PowerPoint window in Slide view. The Outline pane is represented by index number 1, the Slide pane by index number 2, and the Notes pane by index number 3. You can activate a pane by using the Activate method with the appropriate Pane object. The following example switches the view in the active window to Slide view and activates the Outline pane:

With ActiveWindow
    .ViewType = ppViewSlide
    .Panes(1).Activate
End With

To change the setup of the PowerPoint window in Slide view, use the SplitHorizontal property and the SplitVertical property of the Window object. The SplitHorizontal property controls the percentage of the document window's width that the Outline pane occupies, and the SplitVertical property controls the percentage of the document window's height that the Slide pane occupies. The following example sets the Outline pane to take 25 percent of the width of the document window (leaving 75 percent to the Slide pane) and the Slide pane to take 75 percent of the height of the window (leaving 25 percent to the Notes pane):

With ActiveWindow
    .SplitHorizontal = 25
    .SplitVertical = 75
End With

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

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