#!/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;