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?
 
clockwatcherConnect With a Mentor Commented:
Missed a comma,
my $replacements = { 
   'California' => 'CA',
   'Vermont' => 'VT',
   'Virgina' => 'VA',
   'Ohio' => 'OH'
};

Open in new window

0
 
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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
 
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
 
dwcroninAuthor Commented:
thank you, unfortunately I may have asked that question before but I think I finally have the code working.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.