Art Delphi Automation History Home Politics Email me Default Colours Printable Colours
{work in progress}
| Web sites |
For catching PowerPoint events, or general COM concepts, see MS
Visual Basic Programmer's Guide |
Both methods check to see if PowerPoint is already running before starting a new instance.
Opening PowerPoint (early binding)
Before you can use this method, you must have imported the type library (MSPpt8.olb for PowerPoint 97).
One way of starting PowerPoint is to try the GetActiveObject call, to get a running instance of PowerPoint, but put a call to CoApplication.Create in an except clause. But except clauses are slow, and can cause problems within the IDE for people who like Break On Exceptions set to True. The following code removes the need for a try...except clause, by avoiding using OleCheck on GetActiveObject in the case when PowerPoint is not running.
uses Windows, ComObj, ActiveX, OfficePowerPoint_TLB;
var
PowerPoint: _Application;
AppWasRunning: boolean; // tells you if you can close PowerPoint when you've finished
Unknown: IUnknown;
Result: HResult;
begin
AppWasRunning := False;
Result := GetActiveObject(CLASS_Application, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
PowerPoint := CoApplication.Create
else begin
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, PowerPoint));
AppWasRunning := True;
end;
PowerPoint.Visible := TOleEnum(msoTrue);
...
Without using the type library
Automation is so much easier and faster using type libraries (early binding) that you should avoid managing without if at all possible. But if you really can't, here's how to get started:
var
PowerPoint: Variant;
begin
try
PowerPoint := GetActiveOleObject('PowerPoint.Application');
except
PowerPoint := CreateOleObject('PowerPoint.Application');
end;
PowerPoint.Visible := True;
Early binding:
PowerPoint.Quit; PowerPoint := nil;
Late binding:
PowerPoint.Quit; PowerPoint := UnAssigned;
PowerPoint.Presentations.Add(TOleEnum(msoTrue));
The cast to a TOleEnum is simply to avoid the compiler warning about a constant expression violating subrange bounds.If you're using late binding, you can use a plain boolean:
PowerPoint.Presentations.Add(True);
PowerPoint.Presentations.Open('PresName.ppt', msoFalse, msoFalse, msoTrue);
The second parameter specifies whether the presentation should be opened in read-only
mode. If the third parameter is True, an untitled copy of the file is made. The
last parameter specifies whether the opened presentation should be visible. You
can miss these parameters out in late binding if you're happy with the defaults
(False, False, True, respectively, as in the code shown.)
PowerPoint.Save;
Or for SaveAs:
var
EmbedFonts: OleVariant;
begin
EmbedFonts := False;
PowerPoint.ActivePresentation.SaveAs('PresName.ppt', ppSaveAsPresentation, EmbedFonts);
The second parameter for SaveAs determines the save format - possible values
are ppSaveAsAddIn, ppSaveAsPowerPoint3, ppSaveAsPowerPoint4, ppSaveAsPowerPoint7,
ppSaveAsPresentation, ppSaveAsRTF, or ppSaveAsTemplate. These are constants
defined in the type library. If you aren't using the type library, you can define
them yourself like this:
const ppSaveAsPresentation = $00000001; ppSaveAsPowerPoint7 = $00000002; ppSaveAsPowerPoint4 = $00000003; ppSaveAsPowerPoint3 = $00000004; ppSaveAsTemplate = $00000005; ppSaveAsRTF = $00000006; ppSaveAsShow = $00000007; ppSaveAsAddIn = $00000008; ppSaveAsWizard = $00000009; ppSaveAsPowerPoint4FarEast = $0000000A; ppSaveAsDefault = $0000000B;
>>>>>How to close a presentation<<<<<
PowerPoint.ActivePresentation.Close;