Reputation: 19
Need to an open an PowerPoint presentation. I am using the below statement.
Var ppt: _Application; pres: _Presentation;
try
ppt := GetActiveOleObject('PowerPoint.Application') as _Application;
except
ppt := CreateOleObject('PowerPoint.Application') as _Application ;
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open(FPOTX, msoFalse, msoTrue, msoTrue);
except
on e:exception do begin
printtofile('Error in call to ppt.Presentation.Open' + e.message);
end;
end;
It works fine whenever , CreateOleObject() in exception is called. (i.e., no presentation is already open).
But the above statement fails , if one presentation is already open. (i.e, ppt.Presentations.Open() is called after GetActiveOleObject() function).
Using Delphi XE2 , MS Office 2013 , Windows 8
This fails only in Windows 8 not in Windows 7. Thanks in advance.
Upvotes: 0
Views: 917
Reputation: 133
This works for me absolutely without any issues. The problem faced was compatibility issue between the XXX.pot(office 1997-2003) and XXX.potx (office2014). Apart from that everthing was fine.
Upvotes: 0
Reputation: 1871
I don't know where the problem is, if it is your Delphi or Office or Windows version. But this code works no problem in Windows 8.1 x64, Delphi XE2 (32bit target), Office 2007. Unfortunately I do not have Office 2013 to test it.
I don't have any Type LIBs imported in my Delphi. So I tested it just using plain Variant
types.
If PPT isn't opened, the code opens it. Otherwise it gets the OLE Object. Afterwards the desired Presentation is opened. Works as often as I tested it, no matter if PPT is closed or opened.
...
implementation
uses
ComObj, ActiveX;
const
msoFalse = TOleEnum(False);
msoTrue = TOleEnum(True);
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ppt, pres: Variant;
begin
ppt := Unassigned;
pres := Unassigned;
try
ppt := GetActiveOleObject('PowerPoint.Application');
except
ppt := CreateOleObject('PowerPoint.Application');
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open('C:\Temp\Test.pptx', msoFalse, msoTrue, msoTrue);
except
on E:Exception do
ShowMessage('OOPS');
end;
end;
EDIT
I also tested it with an imported PowerPoint Type Lib. And your code works 1:1 here:
...
implementation
uses
ComObj, ActiveX, PowerPoint_TLB;
const
msoFalse = TOleEnum(False);
msoTrue = TOleEnum(True);
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ppt: _Application;
pres: _Presentation;
begin
ppt := nil;
pres := nil;
try
ppt := GetActiveOleObject('PowerPoint.Application') as _Application;
except
ppt := CreateOleObject('PowerPoint.Application') as _Application;
end;
ppt.Visible := msoTrue;
try
pres := ppt.Presentations.Open('C:\Temp\Test.pptx', msoFalse, msoTrue, msoTrue);
except
on E:Exception do
ShowMessage('OOPS');
end;
end;
SOLUTION FOR OFFICE 2013
As you already found out: It seems to work with Office 2013
when changing the Title
parameter of ppt.Presentations.Open
to msoFalse
==> ppt.Presentations.Open(FPOTX, msoFalse, msoFalse, msoTrue)
Upvotes: 1