Perl can do many things beyond normal expectation. One example is with OLE objects. If you have MS Office installed on your computer, you can try the following code. It creates a word document and saves as C:\test.doc. You will see the Word document automatically opening up, adding lines and closing down. This demonstrates the capability of Perl to manipulate MS Office files. Application is such as automatic business report generation.
#!\usr\bin\perl -w
# http://www.adp-gmbh.ch/perl/word.html
# http://www.xav.com/perl/faq/Windows/ActivePerl-Winfaq12.html
# http://www.ngbdigital.com/perl_ole_word.html
use warnings;
use strict;
use Win32::OLE;
my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
$word->{'Visible'} = 1;
my $filename = "C:\\test.doc";
my $document = $word->Documents->Add;
my $selection = $word->Selection;
$selection -> TypeText("Hello HomeTom");
$selection -> TypeParagraph;
$selection -> TypeText("How are you doing today?");
$selection -> TypeParagraph;
$selection -> TypeText("Great. How about you?");
$selection -> {'Style'} = "Heading 1";
$selection -> TypeParagraph;
my $heading_1 = $document->Styles("Heading 1");
my $heading_1_font = $heading_1 -> Font;
$heading_1_font -> {Name} = "Bookmann";
$heading_1_font -> {Size} = 20;
$heading_1_font -> {Bold} = 1;
# Save As
$word->ActiveDocument->SaveAs({FileName => $filename});
$selection -> Typeparagraph;
$selection -> TypeText("Now save and exit after 3 seconds");
sleep 3;
$word->Documents->Close;
$word->Quit;
1;
Wednesday, May 12, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment