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://duckduckgo.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->LastError(); 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) }{'TextToDisplay', 'Address', 'SubAddress'}; @{ $HLs->Item($ihl) }{'Address', 'SubAddress'} = ($url, ''); print STDERR $ihl, $HLs->Item($ihl), @{ $HLs->Item($ihl) }{'TextToDisplay', 'Address', 'SubAddress'}; } $doc->Save; $doc->Close; } $MSWord->Quit; #### 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.org/ 2 Win32::OLE=HASH(0x478d98) second http://duckduckgo.com/ 2 Win32::OLE=HASH(0x478c00) second http://www.perlmonks.org/