Microsoft Office Automation with Visual FoxPro

Getting a handle on the application

Now that you have some background on PowerPoint s object model, type the following command into the Command Window to instantiate PowerPoint using Automation:

oPowerPoint = CreateObject("PowerPoint.Application")

It takes a second for the cursor to come back (it s going to the disk to pull up PowerPoint give it a nanosecond!). PowerPoint is now instantiated. "Yeah, right," you say check the Task List if you don t believe us! Remember that the Office servers instantiate as not Visible. So, type in the following command to see the instance:

oPowerPoint.Visible = .T.

You are now viewing the PowerPoint Application. The variable oPowerPoint is a handle to the PowerPoint Application object. The Application object represents the entire instance of PowerPoint. It knows what presentations are open through the Presentations collection, and it knows which one is active through the ActivePresentation property.

Controlling the size and location of the PowerPoint window is easy with the Top, Left, Height, and Width properties. These are all measured in points. The following code sets the top and left about 1" from the edge of the desktop and makes it about 4" square (remember, there are 72 points to the inch). The inches used to display the windows are virtual inches meaning that they approximate a real inch. However, there are differences in display sizes, which makes a difference in the size of the window. On a display larger than 20", the window is about 5" square and 1.25" from the top and left. On a tiny laptop display, it may only be 3.5". The resolution of the screen is taken into account for virtual inches, so the window should be the same size on any display regardless of the resolution. In any case, the window takes up the same proportions on any screen it s displayed on. If you re following along in the Command Window, be sure your PowerPoint window is set to Normal (as opposed to minimized or maximized) before proceeding.

WITH oPowerPoint

.Top = 72

.Left = 72

.Width = 288

.Height = 288

ENDWITH

If you re following along by typing these commands in the FoxPro Command Window and the PowerPoint window was either minimized or maximized, you get the error: "OLE IDispatch error exception code 0 " PowerPoint doesn t allow you to set these properties if the application is minimized or maximized. When you write your code, you need to check the WindowState property to ensure PowerPoint s window is "normal" (neither maximized nor minimized). The WindowState property uses the following intrinsic constants (see Chapter 2, "The Office Servers," for an explanation of VBA constants): ppWindowNormal (1), ppWindowMinimized (2), and ppWindowMaximized (3). Run the following code to make the window "normal" before setting the height and width:

#DEFINE ppWindowNormal 1

WITH oPowerPoint

IF .WindowState <> ppWindowNormal

.WindowState = ppWindowNormal

ENDIF

ENDWITH

A nice thing to do for your users is to change the caption on PowerPoint to let them know why the PowerPoint window suddenly appeared. Use the Caption property to change the window s title.

oPowerPoint.Caption = "Automated from The World's Greatest Application"

The Quit method closes down the PowerPoint instance. If there are any unsaved presentations open, the user is prompted to deal with them and further processing is suspended until the user answers the prompts. See the next section, "Managing presentations and slides," for information on how to save the presentations before calling the Quit method. This method accepts no parameters. It s simply:

oPowerPoint.Quit

The Quit method works differently in the various Office applications. In Word, you can pass it a parameter to automatically save changes, prompt the user for changes, or quit without saving changes. Excel behaves like PowerPoint, and prompts the user if there are unsaved changes. Outlook just shuts down without saving any changes. Be aware that, despite polymorphism, the Office object models are occasionally inconsistent!

 

Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved

Категории