Are you sure the document in ->Open() exists ?
The tokens must be hyperlinks not just text.
poj
| [reply] [d/l] |
my $doc = $MSWord->Documents->Open('C:/temp/hyper.docm') or
die $!;
As far as the document: my "hyper.docm" was just a very simple test document I created.
This is text
This is hyperlink.
This is text
this is another hyper.
... then on "hyperlink" and "another hyper", I used Word to create hyperlinks (to wherever), saved, and exited.
If you trust me1 , you could run this script to replicate my .docm exactly.
edit: footnote 1: trust is earned. if I haven't earned it, don't run that code; I won't be offended. In general, it's a bad idea to download and run binaries that you have no reason to trust. | [reply] [d/l] [select] |
"Yes, it exists." Weird, not sure why it's not working for you.
Here's an alternative to my base64-encoded .docm: I added the document creation to the Win32::OLE commands. I also was reminded that $! doesn't get set by Win32::OLE; instead, use Win32::OLE->LastError().
use warnings;
use strict;
use Win32::OLE;
use Data::Dumper; $Data::Dumper::Maxdepth = 2;
use constant {
wdCharacter => 1,
wdWord => 2,
wdParagraph => 4,
wdLine => 5,
wdStory => 6,
wdMove => 0,
wdExtend => 1,
};
my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or die Win32:
+:OLE->LastError();
$MSWord->{Visible} = 1;
my $fname = 'c:/temp/hyper2.docx';
{
my $doc = $MSWord->Documents->Add;
my $sel = $MSWord->Selection;
$sel->TypeText("This is the first hyperlink inside a paragraph.");
$sel->TypeParagraph;
$sel->MoveLeft(wdWord, 5, wdMove);
$sel->MoveRight(wdWord, 1, wdExtend);
my $rng = $sel->Range;
print STDERR $sel->Range->Text, $/;
$doc->Hyperlinks->Add({ Anchor => $rng, Address => 'http://google.
+com', TextToDisplay => $rng->Text });
$sel->EndKey( { Unit => wdStory } );
$sel->TypeText("This is the second.");
$sel->TypeParagraph;
$sel->MoveLeft(wdWord, 3, wdMove);
$sel->MoveRight(wdWord, 1, wdExtend);
$rng = $sel->Range;
print STDERR $sel->Range->Text, $/;
$doc->Hyperlinks->Add({ Anchor => $rng, Address => 'http://duckduc
+kgo.com', TextToDisplay => $rng->Text });
$sel->EndKey( { Unit => wdStory } );
$doc->SaveAs2($fname);
print STDERR $fname = $doc->FullName(), $/;
}
{
my $doc = $MSWord->Documents->Open($fname) or die Win32::OLE->Last
+Error();
local $, = "\t";
my $url = 'http://www.perlmonks.org/';
my $HLs = $doc->Hyperlinks();
foreach my $ihl ( 1 .. $HLs->Count ) { # ->Item() is 1-based, not
+ 0-based
print STDERR $ihl, $HLs->Item($ihl), @{ $HLs->Item($ihl) }{'Te
+xtToDisplay', 'Address', 'SubAddress'};
@{ $HLs->Item($ihl) }{'Address', 'SubAddress'} = ($url, '');
print STDERR $ihl, $HLs->Item($ihl), @{ $HLs->Item($ihl) }{'Te
+xtToDisplay', 'Address', 'SubAddress'};
}
$doc->Save;
$doc->Close;
}
$MSWord->Quit;
When I run that exact code, this is what I get. How does it compare to your output for the same code?
inside
second
C:\temp\hyper2.docx
here
1 Win32::OLE=HASH(0x478c00) inside http://google.com/
1 Win32::OLE=HASH(0x4788b8) inside http://www.perlmonks.o
+rg/
2 Win32::OLE=HASH(0x478d98) second http://duckduckgo.com/
2 Win32::OLE=HASH(0x478c00) second http://www.perlmonks.o
+rg/
| [reply] [d/l] [select] |