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;

In reply to Hyperlink Question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.