Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

PowerPoint Help

by Buzz4693 (Initiate)
on Dec 20, 2014 at 13:41 UTC ( #1110915=perlquestion: print w/replies, xml ) Need Help??

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

Hello! I am having trouble converting my PowerPoint VBA script into a Perl script. Here is the VBA script:
Dim slide As slide Dim shape As shape For Each slide In ActivePresentation.Slides For Each shape In slide.Shapes If shape.TextFrame.HasText Then If shape.HasTextFrame Then shape.TextFrame.TextRange.Text = Replace(shape.TextFra +me.TextRange.Text, "PM3", "TCF6203") End If End If Next shape Next slide End Sub
And here is what I have in Perl so far:
use strict; use warnings; use Cwd; use Win32::OLE; use Win32::PowerPoint; use Win32::OLE::Const 'Microsoft Office'; use Win32::OLE::Const 'Microsoft PowerPoint'; use Win32::Process; use Win32::OLE::Enum; $Win32::OLE::Warn = 3; my $file = "PM30306901A SMP Limit Switch.pptx"; my $dir = getcwd(); my $filename = $dir . '\\' . $file; print ("Starting PowerPoint\n"); my $process = Win32::OLE->GetActiveObject('Powerpoint.Application')|| Win32::OLE->new('Powerpoint.Application', 'Quit'); print ( "Opening '$filename'\n" ); my $ppt = $process->Presentations->Open($filename); my $pp = Win32::PowerPoint->new; $file =~ s/PM3/TCF6203/; $filename = "$dir/$file'"; print ( "Replacing 'PM3' with 'TCF6203'\n" ); my @activeslides = $process->ActivePresentation->Slides; foreach my $slide (@activeslides) { foreach my $shape ($slide->Shapes){ if ($shape->TextFrame->HasText){ if ($shape->HasTextFrame){ $shape->TextFrame->TextRange->Text = Replace($shape->T +extFrame->TextRange->Text, "PM3", "TCF6203"); } } } } print ( "Saving '$filename'\n" ); $pp->save_presentation($filename);
The problem occurs when it gets to the foreach loops. It says "Shapes not found in Slides collection." Is there a way to define an object as a shape and slide in Perl? Basically I need help converting the VBA script to the Perl Script.

Replies are listed 'Best First'.
Re: PowerPoint Help
by poj (Abbot) on Dec 20, 2014 at 15:21 UTC
    Use the in iterator.
    #!perl use strict; use warnings; use Cwd; use Win32::OLE; use Win32::OLE::Const 'Microsoft PowerPoint'; $Win32::OLE::Warn = 3; my $file = "PM30306901A SMP Limit Switch.pptx"; my $dir = getcwd(); my $filename = $dir . '\\' . $file; print ("Starting PowerPoint\n"); my $process = Win32::OLE->GetActiveObject('Powerpoint.Application') || Win32::OLE->new('Powerpoint.Application', 'Quit'); print ( "Opening '$filename'\n" ); my $ppt = $process->Presentations->Open($filename); #my $pp = Win32::PowerPoint->new; $file =~ s/PM3/TCF6203/; $filename = "$dir/$file"; my @activeslides = $process->ActivePresentation->Slides->in; foreach my $slide (@activeslides) { my $name = $slide->{Name}; foreach my $shape ($slide->Shapes->in){ if ($shape->TextFrame->HasText){ if ($shape->HasTextFrame){ my $text = $shape->TextFrame->TextRange->Text; print "$name Text $text = "; if ($text eq "PM3"){ $shape->TextFrame->TextRange->{Text}= "TCF6203"; print "TCF6203\n"; } else { print "No change\n"; }; } } } } print ( "Saving '$filename'\n" ); $ppt->SaveAs($filename);
    poj
      Thank you so much for the help! It worked awesomely!
Re: PowerPoint Help
by CountZero (Bishop) on Dec 20, 2014 at 13:57 UTC
    You will probably get quicker and better answers and help, if you explain what kind of trouble you experience.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1110915]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-01-28 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?