in reply to Re: Add hyperlink to paticular lines of MS doc
in thread Add hyperlink to paticular lines of MS doc

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;