Use Word from Perl to create attractive printouts of HTML documents on the fly.
This hack shows you how to use Perl to import an HTML document into Word, tweak the formatting, save the document in native Word format, and print it out to the default printer. This hack assumes you have a file named C:\resumel on your system. It also assumes you have Perl installed on your system and can run Perl scripts from the DOS command line.
To download a free version of Perl for Windows, go to the ActiveState web site at [Hack #84], you can access Word from within a Perl script using the Win32::OLE module, which the ActiveState Perl distribution includes.
The following Perl script creates a new document, inserts some text, and applies the Heading 1 style to the text:
use Win32::OLE qw(in with); use Win32::OLE::Variant; my $word; eval {$word = Win32::OLE->GetActiveObject('Word.Application')}; die "Word not installed" if $@; unless (defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; }) or die "Could not start Word"; } $word->{'Visible'} = 1; my $doc = $word->{'Documents'}->Add; $doc->{'Range'}->InsertAfter('Hello, Word'); my $rng = $doc->{'Range'}; $rng->{'Style'} = 'Heading 1';
Save this script as C:\HelloFromPerl.pl and run it from the DOS command line as follows:
> perl HelloFromPerl.pl
As discussed in [Hack #84], Word objects created as COM servers aren't visible by default. You must explicitly set the Visible property to 1 if you want Word to appear onscreen.
Word does an excellent job of importing HTML filesespecially ones that use simple, standard HTML tags mapped to Word's built-in styles. You can easily translate existing HTML files into a useful printed format by importing them into Word. This process can be automated with Perl and COM.
As an example, this hack will show you this process using an HTML file you might already have, and which is probably more up-to-date than any print version: your resume.
Again, this hack assumes you have a file named C:\resumel on your system. The code presented below starts Word, opens the file, changes the appearance of the Heading 2 and Hyperlink styles, saves the document, and prints it out to your default printer:
use Win32::OLE qw(in with); use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Word'; my $word; eval {$word = Win32::OLE->GetActiveObject('Word.Application')}; die "Word not installed" if $@; unless (defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; }) or die "Could not start Word"; } $word->{'Visible'} = 1; my $confirm = $word->{'Options'}->{'ConfirmConversions'}; $word->{'Options'}->{'ConfirmConversions'} = 0; my $doc = $word->{'Documents'}->Open("C:/resumel"); my $style = $doc->Styles('Heading 2'); $style->{'Font'}->{'Size'} = 16; $style->{'Font'}->{'Italic'} = 0; $style = $doc->Styles('Hyperlink'); $style->{'Font'}->{'Color'} = wdColorAutomatic; $style->{'Font'}->{'Underline'} = wdUnderlineNone; undef $style; $doc->SaveAs("C:/resume.doc", { 'FileFormat' => wdFormatDocument }); $doc->PrintOut( ); $doc->Close( ); $word->{'Options'}->{'ConfirmConversions'} = $confirm; undef $doc; undef $word;
Save this script as resumeprinter.pl and run it from a DOS command line:
> perl resumeprinter.pl
Note that Perl uses different and more cumbersome syntax for handling objects than VBA. For information on working with objects in Perl, check out O'Reilly's Learning Perl Objects, References, and Modules.
A few parts of this script deserve closer attention.
Select Tools
By using the Win32::OLE::Const module, as shown in this script, you can work with Word's constants (such as wdUnderlineNone and wdColorAutomatic) from within a Perl script.
When using Word from Perl, as with VBA, you can use named arguments, which means you can specify the values for a function or method by keyword. When you don't use named arguments, each value passed as an argument must be in a particular order. For example, the syntax for the MsgBox function in VBA is:
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
If you call this function in VBA without using named arguments, the function expects and interprets the values in the order specified by its syntax. To tell the function to display the prompt "Hello, World" with "Message in a Box" as the dialog's title, but without specifying a button type, insert the following:
Msgbox "Hello, World", ,"Message in a Box"
Notice the empty value in between the two commas. It tells Word to use its default value for the buttons argument. If you leave out that empty value, Word tries to use "Message in a Box" as the buttons value, which causes an error. When you use the named-argument syntax in VBA, you can do the same thing in a more readable way, and in any order you choose:
MsgBox Title:="Message in a Box", Prompt:="Hello, World"
Word uses its default settings for any of the arguments not specified. When using Word objects and methods from Perl, you can use a similar syntax, as shown in the following lines taken from the resumeprinter.pl script shown earlier:
$doc->SaveAs("C:/resume.doc", { 'FileFormat' => wdFormatDocument });
|
Ian Burrell and Andrew Savikas