Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Hyperlink Question

by Anonymous Monk
on Sep 27, 2020 at 02:21 UTC ( [id://11122253]=perlquestion: print w/replies, xml ) Need Help??

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

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;

Replies are listed 'Best First'.
Re: Hyperlink Question
by haukex (Archbishop) on Sep 27, 2020 at 17:47 UTC

    This is more of a VBA question than a Perl question, since Win32::OLE basically gives you direct access to that API. The problem seems to stem from how Word internally implements hyperlinks, and retreiving the contents of the document is not as straightforward as character indexes when there are hyperlinks present. For example, if you add print "<",$d->Range(62,111)->{Text},">\n"; to your code after adding the hyperlink, you'll see the output <HYPERLINK "http://www.perlmonks.org" hyperlink>. Perhaps something like this SO answer could be helpful to you in implementing this correctly. Once you have the code figured out in the VBA world, it should be trivial to translate it to Perl.

      Thank you very much for your help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122253]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-23 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found