Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Has anybody succeded in rendering hyperlinks in Microsoft Word? I am using Win32::Word::Writer to generate a report with links. Everything is working fine except for the hyperlinks which are not clickable. Any way around how to get this to work? Thank you guys.
use strict; use Win32::Word::Writer; my $oWriter = Win32::Word::Writer->new(); $oWriter->WriteParagraph("Testing", heading => 1); $oWriter->WriteParagraph("", style => "Heading 9"); $oWriter->NewParagraph(); $oWriter->TableBegin(); $oWriter->TableRowBegin(); $oWriter->TableColumnBegin(); $oWriter->SetBold(0); $oWriter->TableColumnBegin(); $oWriter->Write("Column 1"); $oWriter->TableColumnBegin(); $oWriter->Write("Column 2"); $oWriter->TableColumnBegin(); $oWriter->Write("URL"); my $counter = 0; my @urls = qw (http://ww.perlmonks.org http://www.perl.com http://www. +perl.org); for my $i (0..2}){ $counter++; $oWriter->TableRowBegin(); for my $i (0..1){ $oWriter->TableColumnBegin(); $oWriter->Write("$counter"); } for my $u(@urls){ $oWriter->TableColumnBegin(); $oWriter->SetStyle("hyperlink"); $oWriter->Write("$u"); $oWriter->ClearCharacterFormatting(); } $oWriter->TableEnd(); $oWriter->SaveAs("my_file.doc");

Replies are listed 'Best First'.
Re: Hyperlinks with win32::word::writer
by liverpole (Monsignor) on Jun 03, 2006 at 14:51 UTC
    I've never used Win32::Word::Writer, so excuse me if this doesn't help.  However, reading the documentation at CPAN, I see the line:

        $oWriter->SetStyle("Hyperlink");

    So my recommendation would be to try capitalizing "Hyperlink", and see if that solves the problem.

    If not, tell me where to get Win32::Word::Writer (I don't find it in any of my ppm repositories), and I'll download it and investigate a little more.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Hyperlinks with win32::word::writer
by NetWallah (Canon) on Jun 04, 2006 at 18:02 UTC
    Writer is painful to install. Anyway, here is working hyperlink code (after fixing syntax errors in your posted code). The mechanism to add hyperlinks is more than just STYLE - you need to use the SELECTION syntax - perhaps subclass Writer to provide more ease-of-use. The crazy NULL write seems to be required by WORD to create the hyperlink. Ayyway, this code now works for me.
    use strict; use Win32::Word::Writer; my $oWriter = Win32::Word::Writer->new(); $oWriter->WriteParagraph("Testing", heading => 1); $oWriter->WriteParagraph("", style => "Heading 9"); $oWriter->NewParagraph(); $oWriter->TableBegin(); $oWriter->TableRowBegin(); $oWriter->TableColumnBegin(); $oWriter->SetBold(0); $oWriter->TableColumnBegin(); $oWriter->Write("Column 1"); $oWriter->TableColumnBegin(); $oWriter->Write("Column 2"); $oWriter->TableColumnBegin(); $oWriter->Write("URL"); my $counter = 0; my @urls = qw (http://ww.perlmonks.org http://www.perl.com http://www. +perl.org); for my $i (0..2){ $counter++; $oWriter->TableRowBegin(); for my $i (0..1){ $oWriter->TableColumnBegin(); $oWriter->Write("$counter"); } for my $u(@urls){ $oWriter->TableColumnBegin(); #$oWriter->SetStyle("hyperlink"); #$oWriter->Write("$u"); $oWriter->Write (""); # Null write, to make a selection my $sel= $oWriter->oSelection; $sel->Hyperlinks->Add( $sel->Range, $u); # $oWriter->ClearCharacterFormatting(); } } $oWriter->TableEnd(); $oWriter->SaveAs("my_file.doc");

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

      You're right Netwallah -- Writer is such a pain to install. I had to resort to using the CPAN shell (perl -MCPAN -e shell). Thank you guys for your help. I would never have thought about using the oSelection property. I have revised the script to create a cleaner table. You guys rock.
      use strict; use Win32::Word::Writer; my $oWriter = Win32::Word::Writer->new(); $oWriter->WriteParagraph("Testing", heading => 1); $oWriter->WriteParagraph("", style => "Heading 9"); $oWriter->NewParagraph(); $oWriter->TableBegin(); $oWriter->TableRowBegin(); #$oWriter->TableColumnBegin(); $oWriter->SetBold(0); $oWriter->TableColumnBegin(); $oWriter->Write("Column 1"); # $oWriter->TableColumnBegin(); $oWriter->Write("Column 2"); $oWriter->TableColumnBegin(); $oWriter->Write("URL"); my $counter = 0; my @urls = qw (http://ww.perlmonks.org http://www.perl.com http://www. +perl.org); for my $i (@urls){ $oWriter->TableRowBegin(); for my $j (0..1){ $counter++; $oWriter->TableColumnBegin(); $oWriter->Write("$counter"); } # for my $u(@urls){ $oWriter->TableColumnBegin(); # #$oWriter->SetStyle("hyperlink"); # #$oWriter->Write("$u"); $oWriter->Write (""); # Null write, to make a selection my $sel= $oWriter->oSelection; $sel->Hyperlinks->Add( $sel->Range, $i); # #$oWriter->ClearCharacterFormatting(); # } } $oWriter->TableEnd(); $oWriter->SaveAs("my_file.doc");