in reply to M$ Word ole hyperlink

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

Replies are listed 'Best First'.
Re^2: M$ Word ole hyperlink
by 3dbc (Monk) on Jan 31, 2017 at 18:00 UTC
    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

        I agree, it's probably in the ->Open(). This might help 3dbc debug it.

        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.

        Yes, it exists.
        - 3dbc