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

In reply to M$ Word ole hyperlink by 3dbc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.