Hi VincentK,

Your solution has helped me a lot for adding links on new MS Doc

I tried to add hyperlink to existing document by opening it, then selecting the text on which i want to add hyperlink. Selection of sentence is visible, but giving error "Can't call method "Hyperlinks" on an undefined value" while applyinh hyperlink. I want to add hyperlink on docx file sentence '1.1.1lc_base_dpp0'

I am not understanding why it is not applying hyperlink on selected sentence

Docx file is having following content

Line 1

Line2

Line3

Line4

1.1.1lc_base_dpp0

Line2

I am providing my sample code

use strict;
use Win32::OLE;
use Win32::OLE::Enum;
use Win32::OLE qw(in);
use Data::Dumper;
use Win32::OLE::Const 'Microsoft Word';
my $Word = Win32::OLE->GetActiveObject('Word.Application');
unless ($Word) { $Word = Win32::OLE->new('Word.Application', sub {$_[0 +]->Quit;}) or die "oops\n"; } <code>$Word->{visible} = 1;

my $file_name_for_mod_spec = "D:/Perl_programs/test1.docx";
my $generated_doc2 = $Word-> Documents->Open("$file_name_for_mod_spec");
my $generated_doc_paragraphs1 = $generated_doc2->Paragraphs;
my $generated_doc_total_paragraphs1 = $generated_doc_paragraphs1->Count ();
for my $p11 (1..$generated_doc_total_paragraphs1)
{
        my $template_paragraph1 = $generated_doc_paragraphs1->Item ($p11);
        my $template_text = $template_paragraph1->{Range}->{Text};
        if ($template_text =~ m/1.1.1lc_base_dpp0/gi)
        {
            my $mod_selection = $template_paragraph1->{Range}->Select();
            sleep(5);
            print " 11111 $p11 $template_text \n";
            $mod_selection->Hyperlinks->Add({ Anchor => $generated_doc_paragraphs1->Range->Sentences($p11), Address => "http://perlmonks.org" }) or die "I am unable to do it";
            sleep(5);
        }
}
$generated_doc2->Close();
$Word->Quit();
undef $generated_doc2;
undef $Word;

In reply to Re^2: Add hyperlink to paticular lines of MS doc by Akku
in thread Add hyperlink to paticular lines of MS doc by Akku

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.