Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I know that my question is not so relevant for Perl, but I really can't find any other information on the Web, so I hope to fine a monk wanting to help me out.

I need to insert in a Word document comments for words matching a RegExp. So far I put together the following script which is working fine. It highlight all instances of that word in the document. What I'd like to do is to add a comment for that words. I don't find any info for that. Can you point me to the right direction?

#!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; my $word = get_word(); $word->{Visible} = 1; my $doc=$word->Documents->Open('D:/test.docx'); my $WordToLookup="xy"; FindAndReplace(); sub FindAndReplace{ $word->Selection->HomeKey(wdStory); $word->Selection->Find->{'Text'}=$WordToLookup; $word->Selection->Find->Replacement->{'Highlight'}=1; $word->Selection->Find->Execute({Replace=>wdReplaceAll}); } sub get_word { my $word; eval { $word = Win32::OLE->GetActiveObject('Word.Application'); }; die "$@\n" if $@; unless(defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit +}) or die "Oops, cannot start Word: ", Win32::OLE->LastError, "\n"; } return $word; }

Replies are listed 'Best First'.
Re: Word OLE add Comments
by Corion (Patriarch) on Oct 23, 2014 at 17:09 UTC
Re: Word OLE add Comments
by poj (Abbot) on Oct 23, 2014 at 18:56 UTC
    sub FindAndReplace{ my $count = 0; my $s = $word->Selection; $s->HomeKey(wdStory); $s->Find->{'Text'}=$WordToLookup; $s->Find->Replacement->{'Highlight'}=1; while ( $s->Find->Execute() ){ $s->Comments->Add({ Range=>$s->Range, Text =>"Match ".++$count }); } $s->HomeKey(wdStory); }
    poj
Re: Word OLE add Comments
by perlron (Pilgrim) on Oct 24, 2014 at 00:00 UTC
    Dear Anonymous Monk, one option i would try in parallel is checking with the Perl-Win32-Users mailinglist at ActiveState.com, who from the looks of it on metacpan, have the rights for this module. They might be able to answer a query , wether comments can be added to a Word Document.
      They may have the rights for the module, but the Word OLE-Interface is from Microsoft. His question had nothing to do with sending the commands to Word (that would be OLE), but Word-specific commands implemented in Word's OLE interface.