in reply to Re^4: M$ Word ole hyperlink
in thread M$ Word ole hyperlink
"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/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: M$ Word ole hyperlink
by 3dbc (Monk) on Feb 01, 2017 at 18:02 UTC | |
by pryrt (Abbot) on Feb 01, 2017 at 19:46 UTC | |
by 3dbc (Monk) on Feb 02, 2017 at 23:00 UTC |