Hello Monks, I am somewhat new to Perl but am trying to learn it because I have seen several examples over the years of what this language is capable of, it is very powerful in the right hands. I am trying to use Win32::OLE and am working with an example I found on your website so the code may look somewhat familiar. All I am doing is creating a word document, writing and formatting 3 lines, then writing a hyperlink, then writing the 3 lines again. For some reason, the calculation of the range object AFTER the hyperlink is formatted is not calculated correctly even though it is exactly the same code that I used BEFORE formatting the hyperlink (that worked correctly). Can you please enlighten me as to what I am missing? Thank you in advance!
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;
my $word = Win32::OLE->new('Word.Application') or die $!;
$word->{'Visible'} = 1;
# Create new document
my $d = $word->Documents->Add;
# define selection
my $s = $word->Selection;
my @lines = (
"This is test line 1",
"This is test line 2",
"This is test line 3",
);
# $c is the color
# $start is the start of Range
# $end is the end of Range
# $r is the Range object
my ($c, $start, $end, $r) = (2, 0, 0, );
# Write out the @lines
for my $line (@lines) {
$start = ($d->Characters->Count)-1;
$end = length($line)+ $start;
$s->TypeText($line);
# define the Range
$r = $d->Range($start, $end);
# Set font properties
$r->Font->{Size} = 18;
$r->Font->{ColorIndex} = $c++;
$r->Font->{Name} = 'Courier New';
$s->TypeText("\n");
}
# Now insert a hyperlink
my $line = 'hyperlink';
$start = ($d->Characters->Count)-1;
$end = length($line) + $start;
$s->TypeText($line);
# define the Range
$r = $d->Range($start, $end);
# Set font properties
$r->Font->{Size} = 12;
$r->Font->{Name} = 'Arial';
$r->Font->{Bold} = 1;
$d->Hyperlinks->Add({Anchor => $r,
Address => "http://www.perlmonks.org"});
$s->TypeText("\n");
# Now write out the @lines again
for my $line (@lines) {
$start = ($d->Characters->Count)-1;
$end = length($line)+ $start;
$s->TypeText($line);
# define the Range
$r = $d->Range($start, $end);
# Set font properties
$r->Font->{Size} = 18;
$r->Font->{ColorIndex} = $c++;
$r->Font->{Name} = 'Courier New';
$s->TypeText("\n");
}
$word->WordBasic->FileSaveAs("c:\\test.doc");
# house keeping, clean up our instances
$d->Close();
$word->Quit();
undef $word;