3dbc has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brilliant Monks,

Problem; need to update unique hyperlinks in over 160 M$ word documents. I need to be able to search for #hyperlink# tokens in the word doc and replace them with the unique hyperlink I'm composing within my code. Despite my efforts, I can't seem to search for text in the doc with OLE and replace it with a hyperlink, like I can with other text in the doc, where I'm pulling data from RDBMS and LDAP and updating this info into word doc template #tokens# precisely where they need to be in the doc.

I.E. This works, but in this example, it's just putting a basic hyperlink in a new blank word doc, there's no control as to where it's going.
#!/usr/bin/perl use strict; use warnings; use Win32::OLE; my $word = Win32::OLE->new('Word.Application') or die $!; $word->{'Visible'} = 1; my $document = $word->Documents->Add; my $selection = $word->Selection; $selection->TypeText("Click here"); my $range = $word->ActiveDocument->Content; $document->Hyperlinks->Add({ Anchor => $range, Address => "http://www.perlmonks.org" });

However, I want to search, delete and insert the hyperlink at the target location, something like this (but can't get it to work)
$sel->Find->{Text} = '##HYPERLINK##'; $sel->Find->Execute; $sel->Delete; #$sel->InsertAfter->Hyperlinks->Add({("$link")}); my $hyperlink = "http://www.perlmonks.org/"; $sel->TypeText("Sharepoint Link"); my $range = $MSWord->ActiveDocument->Content; $doc->Hyperlinks->Add({ Anchor => $range, Address => "$hyperlink" }); $sel->MoveDown({Count=>1});


I think this would help the community, because there's not much on the web how to do this and I'm sure I'm not the first or last person to try this using perl. Some win32 ole wizardry from the monastery ole masters would be greatly appreciated.
- 3dbc

Replies are listed 'Best First'.
Re: M$ Word ole hyperlink
by poj (Abbot) on Jan 31, 2017 at 08:17 UTC

    Try

    #!/usr/bin/perl use strict; use warnings; use Win32::OLE; my $MSWord = Win32::OLE->new('Word.Application') or die $!; $MSWord->{'Visible'} = 1; my $doc = $MSWord->Documents->Open("c:/temp/Word.doc"); my $sel = $MSWord->Selection; for my $n (1..2){ $sel->Find->{Text} = "##HYPERLINK$n##"; next unless $sel->Find->Execute; $sel->Delete; $sel->Hyperlinks->Add({ TextToDisplay => "Sharepoint Link $n", Anchor => $sel->Range, Address => "http://www.perlmonks.org/" }); $sel->MoveDown({Count=>1}) }
    poj
      This worked very well, thanks so much.
      - 3dbc
Re: M$ Word ole hyperlink
by pryrt (Abbot) on Jan 31, 2017 at 14:48 UTC

    Instead of using Find (like ++poj suggested), you could also iterate thru all the Item()s in the Hyperlinks() collection. Instead of deleting each hyperlink and adding a new one, you could just edit the existing one. (If necessary, it would be easy to add in decision logic on each hyperlink.)

    use warnings; use strict; use Win32::OLE; use Data::Dumper; $Data::Dumper::Maxdepth = 2; my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or die $!; $MSWord->{Visible} = 1; my $doc = $MSWord->Documents->Open('C:/temp/hyper.docm'); local $, = "\t"; my $url = 'http://www.perlmonks.org/'; my $HLs = $doc->Hyperlinks(); foreach my $ihl ( 1 .. $HLs->Count ) { # ->Item() is 1-based, not 0-b +ased print STDERR $ihl, $HLs->Item($ihl), @{ $HLs->Item($ihl) }{'TextTo +Display', 'Address', 'SubAddress'}; @{ $HLs->Item($ihl) }{'Address', 'SubAddress'} = ($url, ''); print STDERR $ihl, $HLs->Item($ihl), @{ $HLs->Item($ihl) }{'TextTo +Display', 'Address', 'SubAddress'}; } $doc->Save; $doc->Close;

    There may not be much on the web for how to do MS Word OLE thru perl, but since the OLE uses the same API as Word's embedded VBA, I just searched for "word vba search for hyperlink", where I found this stackoverflow answer, or "word vba edit hyperlinks", resulting in this answer, and looked at the MSDN documentation for the Hyperlinks collection and Hyperlink object

      Thanks for the help, but am getting this error when running your code.

      Can't call method "Hyperlinks" on an undefined value at sharepoint.pl line 14.

      Like the approach, but do you have an example word doc you're using, how is it finding the hyperlink tokens?
      Thanks,
      - 3dbc

        Are you sure the document in ->Open() exists ?

        The tokens must be hyperlinks not just text.

        poj