in reply to Add hyperlink to paticular lines of MS doc

Hi Akku.

I used the following Microsoft pages to help me modify your code.

http://msdn.microsoft.com/en-us/library/xa46twee.aspx

http://msdn.microsoft.com/en-us/library/tx0x9x4d.aspx

This issue with your code was that your range was not selected for the current sentence that was written out.

I hope this helps.

use strict; use warnings; use Win32::OLE; my $word = Win32::OLE->new('Word.Application') or die $!; my $document = $word->Documents->Add; my $selection = $word->Selection; $word->{'Visible'} = 1; my @DATA_array = ( 'This is a test line' , 'This is second test Line' , 'This is the third line' ); my ($ColorIndex_iter, $start, $end, $current_sentence_iter) = (2, 0, 0 +, 1); foreach my $elements (@DATA_array) { $selection->TypeText($elements); print "$elements $start ".length($elements)."\n"; $selection->Font->{Size} = 12; $selection->Font->{ColorIndex} = $ColorIndex_iter; $selection->Hyperlinks->Add({ Anchor => $document->Range->Sentence +s($current_sentence_iter), Address => $elements }) or die "I am unabl +e to do it"; $selection -> TypeParagraph; $current_sentence_iter++; sleep (1); } undef $word;

Replies are listed 'Best First'.
Re^2: Add hyperlink to paticular lines of MS doc
by Akku (Novice) on Dec 11, 2013 at 09:23 UTC

    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;