in reply to How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?

The solution you have posted is somewhat clunky - in that it does not read like "normal" perl.

Here is a tested, more perlish alternative:

use strict; use warnings; use Win32::OLE qw(in); # "in" provides the enumeration mechanism# use Win32::OLE::Const 'Microsoft Word'; my $file = 'C:\Users\Netwallah\My Documents\test.docx'; my $WordApp = Win32::OLE->new('Word.Application', 'Quit'); #$WordApp->{'Visible'} = 0; my $doc = $WordApp->Documents->Open($file) or die("Unable to open document '$file':", Win32::OLE->LastError()); for my $para (in $doc->Paragraphs()){ for my $CurrentWord (in $para->{Range}->Words()){ next unless $CurrentWord->Text() =~/perl/i; $CurrentWord->Font->{Bold} = 1; } } $doc->Save(); $WordApp->ActiveDocument->Close ; $WordApp->Quit;

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

  • Comment on Re: How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?
  • Download Code

Replies are listed 'Best First'.
Re^2: How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?
by Anonymous Monk on Jul 26, 2011 at 05:04 UTC
    hi i am facing the same problem ... is there any win32:OLE extra line we need to add for docx file,actually the perl installed on winxp but the ms office plug in for docx is no there. is that a problem>? thanking sudeep
      Which "same" problem are you facing ? Please state your problem.

      In order to use this (Win32::OLE based) mechanism to manage MS office documents, you will need to install the appropriate software - in this case - MS Word, on the machine where you run the code that calls Win32::OLE. If you install Word 2003, you need to install additional MS plug-ins so it can manage "docx" files.

                  "XML is like violence: if it doesn't solve your problem, use more."