use Perl to change Microsoft Word documents

Experts,
I have many Microsoft Word documents that require similar changes.  I would like to use Perl to make these changes.  Unfortunately, I asked this question before and I accepted the answer before I found out that the answer didn't work (more to the point,I couldn't figure out how to make the changes to make it work).I'll wait until the files actually change before I accept the solution this time.
I am using Windows XP, Microsoft Word 2002, the files are .doc files, the changes are things like using the postal abbreviations for the state names, the names of the files are quite varied but they're all in the same directory.
Thanks
dwcroninAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Did you resolve the issue in your last post in the previous question with wilcoxon's response?
0
dwcroninAuthor Commented:
No I didn't.  I can get the Perl package manager to work.  At least I think I can since that is what it's window is named.

on April 4, 2011: I mistakenly accepted the solution before I found out that I can't get it to work.  I think that I have Perl package manager on my machine, but I don't see text:: extract:: word.

That's my first problem, after that I still don't know what script I should use.

0
käµfm³d 👽Commented:
on April 4, 2011: I mistakenly accepted the solution before I found out that I can't get it to work.  I think that I have Perl package manager on my machine, but I don't see text:: extract:: word.

That's my first problem, after that I still don't know what script I should use.
I understand that the answer you picked in the previous question didn't *exactly* work, but what I'm referring to is what wilcoxon mentions in his last post regarding the installation of the Text::Extract::Word package. Are you saying that the installation commands he offered didn't work either? (I realize you said that you didn't see the package listed in the package manager, but I think he offered you alternative ways to install it.)
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

clockwatcherCommented:
Since you're on Windows, have Word installed on the machine that you're going to be running this on and don't have a problem with running this interactively, I'd suggest automating Word with Win32::OLE.  The modules that wilcoxon is suggesting are great if you don't have Word available to you, but since you do, automation is probably a much better bet.  You won't lose any formatting (which in a typical word processing file would usually be a pretty big deal) nor do you have to worry about any idiosyncrasies the writer or parser might have.   You also don't need to install any additional modules.  Win32::OLE comes installed with ActiveState.

package WordReplace;

   use Win32::OLE;
   use Win32::OLE::Const 'Microsoft Word';

   sub new {
      my $self = {};

      $self->{'word'} = Win32::OLE->new("Word.Application");
      $self->{'word'}->{'Visible'} = 1;

      bless $self;
      return $self;
   }

   sub replace {

      my ($self, $fname, $saveas, $replacements) = @_;
      my $word = $self->{'word'};

      my $doc = $word->Documents->Open($fname);
      my $replacer = $word->{'Selection'}->{'Find'};
      
      while ( my ($find, $replace) = each(%$replacements) ) {  
         $replacer->{'Text'} = $find;
         $replacer->{'Replacement'}->{'Text'} = $replace;
         $replacer->{'Forward'} = 1;
         $replacer->{'Wrap'} = wdFindContinue;
         $replacer->Execute( { Replace => wdReplaceAll } );
      }

      $doc->SaveAs($saveas);
      $doc->Close();

   }

   sub close {

      my $self = shift;
      $self->{'word'}->Quit();

   }


package main;

use strict;
use Cwd;
use File::Find;

my $replacements = { 
   'California' => 'CA',
   'Vermont' => 'VT',
   'Virgina' => 'VA' 
};
  

my $word = new WordReplace();

my $dir = getcwd;

# word isn't as friendly with slashes as perl is
$dir =~ s#/#\\#g;

my @files = glob("*.doc");

for my $fname (@files) {

   next if $fname =~ /-updated\.doc/;

   $fname = "$dir\\$fname";
   my $saveas = $fname;
   $saveas =~ s/\.doc$/-updated.doc/;

   $word->replace($fname, $saveas, $replacements);
}

$word->close();

Open in new window

0
dwcroninAuthor Commented:
I have a feeling that it's doing the proper thing but I don't really understand what's going on.I'm trying to do a simple substitution but I don't understand this script.  I'm trying to do something like:

Ohio->OH

Where would I put that in this script?
0
clockwatcherCommented:
You'd modify this section:

my $replacements = { 
   'California' => 'CA',
   'Vermont' => 'VT',
   'Virgina' => 'VA' 
};

Open in new window


To add Ohio:

my $replacements = { 
   'California' => 'CA',
   'Vermont' => 'VT',
   'Virgina' => 'VA' 
   'Ohio' => 'OH'
};

Open in new window

0
clockwatcherCommented:
Missed a comma,
my $replacements = { 
   'California' => 'CA',
   'Vermont' => 'VT',
   'Virgina' => 'VA',
   'Ohio' => 'OH'
};

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dwcroninAuthor Commented:
thank you, unfortunately I may have asked that question before but I think I finally have the code working.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.