smithwt has asked for the wisdom of the Perl Monks concerning the following question:

I have looked high and low, but cannot find any appreciable information on the innards of Powerpoint as related to interfacing via Perl. Via Perl, I am able to open a Powerpoint document but cannot do anything else. I have a requirement to paste Excel selected charts into specific ppt slides, resize them and save the ppt file out. I am very conversant on the Excel side as there are many examples and good support there. Powerpoint is an intirely different challenge. I been through the "just launch VBA and convert the code" route with no avail. Any help would be much appreciated! Bill Smith

Replies are listed 'Best First'.
Re: Powerpoint OLE
by rchiav (Deacon) on Sep 08, 2001 at 19:58 UTC
    Armed with Win32::OLE and the Powerpoint object model you should have enough to do what you want to do. The tougher part is weeding through the information there. Also, if you have the Activestate installation of Perl, you can find an OLE browser in the help files.

    Hope this helps..
    Rich

      Rich,

      Thanks for the links. I still am having problems getting off the dime. I'm running the following code which opens Powerpoint, but goes no further:

      use Win32::OLE; use Cwd; use File::Copy; $cwd = cwd(); $input = 'input.xls'; $input_file = "$cwd\\Metrics_Data.txt"; copy ("Template1.xls", "$input"); print "Template file is: $template1\n\n"; print "Input file is: $input\n\n"; open INPUT_FILE, $input || die "No file\n"; $PptApp = Win32::OLE->new ('Powerpoint.Application'); $PptApp->{Visible} = 1; $PpptApp = Presentations(Presentation1)->Open("$input"), ReadOnly ->Tr +ue;
      I made the following ppt macro and am trying to convert it to Perl:
      Sub Open_Slide() ' ' Macro recorded 9/8/2001 by William T. Smith ' Presentations.Add WithWindow:=msoTrue ActiveWindow.View.GotoSlide Index:=ActivePresentation.Slides.Add(I +ndex:=1, Layout:=ppLayoutBlank).SlideIndex End Sub
      If you have any sample code to get me over the initial hump, I'll be flying...

      Bill

        OK.. don't take this too harshly, because it's all meant in the spirit of learning. The problem here is that you have quite a bit to learn about OLE with Perl in general. There's various things you're doing that just won't work.

        First, you need to read the information about the OLE objects for powerpoint. I've never done any OLE Automation with Powerpoint, but this is what I've gathered..

        Let's take a look at the following line..

        $PpptApp = Presentations(Presentation1)->Open("$input"), ReadOnly ->Tr +ue;
        The first thing here is that you're not specifying where "Presentations(..)->Open is. Presentations is a member of PptApp. You have to remember that what you're doing is creating an instance of powerpoint here. When you write a macro, you're already in powerpoint so you don't have to tell it that it's using itself. OLE gives you the ability to use functions that other apps export outside themselves. So so for our purposes, we'll change this to
        $PpptApp = $PptApp->Presentations(Presentation1)->Open("$input"), Read +Only ->True;
        This still won't work though.
        Now look at this which describes the Presentation collection.
        In that, you'll see..

        Use Presentations(index), where index is the presentation's name or index number, to return a single Presentation object.

        This is referring to the presentations that are already in the collection. Currently, you have no presentations, so you can't refrence one. What you want to do though is add one. Actually you want to open an existing one and add it to the collection. So that leads us to this, which describes the Open functionality. You'll see..

        expression.Open(FileName, ReadOnly, Untitled, WithWindow)
        expression Required. An expression that returns a Presentations collection.

        OK.. so we also know that Presentations returns a Presentation Collection. So that would now make out line..

        $PpptApp = $PptApp->Presentations->Open("$input"), ReadOnly ->True;
        Now.. this will actually open the file.. but it's still not correct. If you look back at the information on Open, you'll see that it takes 4 arguments. The last 3 are optional. Respectivly they are "Readonly", "Untitled" and "WithWindow". In Perl, there's no True and False keywords.. but 0 (false) and 1(true) will suffice. So we now change the line to..
        my $PpptApp = $PptApp->Presentations->Open("$input", 1);
        and it does what we want it to do.

        That should also help you figure out how you have to format other things to get them to work correctly.

        Hope this helps..
        Rich

      Rich, I noticed that I sent you the wrong snippit. The "xls" extensions, of course, will not work. I cleaned up my stupitiy, but still can't get Powerpoint to launch on a given ppt file. Do you (or anyone else) have example code I could review/learn from? Tnx! Bill
Re: Powerpoint OLE
by cacharbe (Curate) on Sep 09, 2001 at 03:52 UTC
    The following Works okay for me:
    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft PowerPoint'; #Must Be capitalized to load constants--^ my $PptApp = Win32::OLE->GetActiveObject('PowerPoint.Application')|| Win32::OLE->new('PowerPoint.Application', 'Quit'); $PptApp->{Visible} = 1; my $Presentation = $PptApp->Presentations->Open({FileName=>'<SOMEFIL +ENAME>',ReadOnly=>1});

    Now, please notice that the constants line REQUIRES you to use a capital, but the GetActiveObject and new do not necessarily require capitalization.

    I have a sinking suspicion your problem lies in how you utilizied Open.

    Hope this helps.

    Quick Note: Also, you can't pass True, It's a type mismatch (You gotta flip the bool bit with an integer, which is different than flipping the bozo bit, but sometimes, not by much)

    C-.

      Thanks Rich / C-... I can now open a given Powerpoint file. I ran the code you sent me and it opens an existing ppt file. My next (and most frustrating) Perl challenge is to select a given slide, then paste an Excel chart to the slide. I also need to properly size the chart, but I think that will become trivial if I can just get over the first hill. The perl / Excel side of the problem is already functioning, with my code copying the Excel chart to the clipboard. I've captured a number of Powerpoint macros that do the required functions (slide select, copy, size, et al.) and tried to convert them to Perl to no avail. Does anyone have an end-to-end example of a functioning Perl / Powerpoint app that I can study? I have raided every bookstore in town and have a Perl library at least 3 feet wide. Unfortunately, the Win32 / OLE sections concentrate on Excel, with some mention on Word and Outlook. I have yet to see anything on Powerpoint. If I ever figure it out, maybe I'll write that first Perl / Powerpoint book. If this were just a hobby I'd attack the problem differently, spending the time to get a full indoctrination in OLE. Unfortunately, I'm under the gun to produce an automation product involving Powerpoint in the next week for work. Any thing you can do will be a great help! Bill
        It looks to me (Just from reading your post, and looking at what little code you've provided) that you're a little confused about using this particular object model. You should post some of the things you have tried, and maybe we can help you out.

        So you get an idea of the syntax, I pounded this out this morning over coffee while I caught up on week-end email.

        use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft PowerPoint'; $Win32::OLE::Warn = 3; # Die on Errors my $PptApp = Win32::OLE->GetActiveObject('PowerPoint.Application')|| +Win32::OLE->new('PowerPoint.Application', 'Quit'); $PptApp->{Visible} = 1; my $Presentation = $PptApp->Presentations->Add(); #my $Presentation = $PptApp->Presentations->Open({FileName=>'<SOMEFILE +NAME>',ReadOnly=>1}); my $Slide = $Presentation->Slides->Add({Index=>1 , Layout=>ppLayoutTex +t}); $Slide->{Name} = "Slide1"; my $TextBox=$Slide->Shapes->AddTextbox({Orientation=>1, Left=>5, Top=>5, Width=>250, Height=>250,}); $TextBox->TextFrame->TextRange->{Text} ="Big Ole Test"; my $Title=$Slide->Shapes->{Title}; $Title->TextFrame->TextRange->{Text} ="Title Test"; my $NewSlide = $Slide->Duplicate(); $NewSlide->{Name} = "Slide2";

        I would suggest that you start PowerPoint and leave it running without any active presentations so that you can watch what is going on. Also, look again at how I call methods that require multiple inputs (Hash format vs. List format), and you need to pay close attention to what certain methods and functions return when called.

        C-.