Link to home
Start Free TrialLog in
Avatar of Aqueath
Aqueath

asked on

Powerpoint Automation

Hi,
I'm trying to open an existing powerpoint file, and change the values within a datasheet in one of the slides. Anyone got a working sample of code that does this? Ie

-Open an instance of powerpoint in the background
-Open an existing ppt file
-Change the values within a graphs datasheet.
-Add some text somewhere
-Save the file, and close pp.


A.
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Record a macro to do it and then visit this site to see how to access PP in Delphi:
http://www.djpate.freeserve.co.uk/AutoPpt.htm

Then just convert the VBA macro source to Delphi.

If you're not up to it, post a sample presentation that we can muck with...
Avatar of Aqueath
Aqueath

ASKER

Ok, a sample presentation? Open PP.
-Select New->The New slide window opens
-Select graph
-Doubleclick the graph->you get a sample graph with datasheet.
-Save as...

And you have a sample presentation to muck with...
Using PP2002 so it is different.
Avatar of Aqueath

ASKER

Hi Eddie,
Found an example for you. It can be downloaded from:

http://portfolio.educ.kent.edu/studentj/equity.ppt 

On slide 6 there is a graph.

Hope to hear from you soon.

A.
Avatar of Aqueath

ASKER

In VB this would look something like ;

Private pp As PowerPoint.Application
Private pr As PowerPoint.Presentation
Private ds As Graph.DataSheet


    Set pp = CreateObject("PowerPoint.Application")
    Set pr = pp.Presentations.Open('d:\equity.ppt')

    Set ds = pr.Slides(6).Shapes(2).OLEFormat.Object.Application.DataSheet //This line I need converted from VB to Delphi

    ds.Cells(1, 3) = "Test"
    ds.Cells(2, 3) = 22
    ds.Cells(3, 3) = 3
    ds.Cells(4, 3) = 72

    ds.Application.Update

In Delphi so far I have:

uses ComObj;

...


procedure TForm1.Button1Click(Sender: TObject);
var
  pp : Variant;
  pr : Variant;
  ds : Variant; //do I need some other declaration?
begin
  try
    pp:=GetActiveOleObject('PowerPoint.Application');
  except
    pp:=CreateOleObject('PowerPoint.Application');
  end;
  pp.Visible := True; //I can't seem to get the next line of code to work without visible set to true.
  pr:=pp.Presentations.Open('d:\equity.ppt'); //File can be downloaded from the link in the previous comment
  ds:=pr.Slides.item(6).shapes.item(2).OleFormat; //This is where I'm currently stuck converting from VB.

...

  //ds.Application.Update; //?
end;

I'm no good in VB, so could someone tell me how to convert the line...

Set ds = pr.Slides(6).Shapes(2).OLEFormat.Object.Application.DataSheet

...from VB to delphi? I have so far

 ds:=pr.Slides.item(6).shapes.item(2).OleFormat;


Anyone?

A.


ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aqueath

ASKER

Thanks!

Your code so helped me in the right direction. Even made it work without the TLBs as well....Points!!!

Working example code

procedure TForm1.Button1Click(Sender: TObject);
var
  pp : OleVariant;
  pr : OleVariant;
  ds : OleVariant;
  qs : OleVariant;
begin
  try
    pp:=GetActiveOleObject('PowerPoint.Application');
  except
    pp:=CreateOleObject('PowerPoint.Application');
  end;
  pp.Activate;
  pr:=pp.Presentations.Open('d:\equity.ppt');
  ds:=pr.Slides.Item(6).Shapes.Item(2).OleFormat.object;
  qs:=ds.Application.Datasheet;
  qs.cells.item[2,2]:='Blue';
end;


Would you also know how to run PP in the background? If I never set pp to visible I keep getting this error ;

"The powerpoint frame window does not exist."

A.