D
deepakp
I'm running perl 5.80 on windows XP. I'm looking for an example script
(or help improve my existing script) to update a Microsoft Word
document.
The following script searches for some text in the body of a Microsoft
Word document and replaces it with some other text. It does not update
text appearing the header or footer of a Word document. If anyone can
help modify it update text in header and in footer, I would really
appreciate it.
Thank you,
Deepak
#!c:/perl/bin/perl
####!/usr/bin/perl -w
# FILE: WordOLE.pl
# PURPOSE: Search/Replace a string in a word document
use strict;
use File::Copy;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
use Cwd;
my $this_dir = cwd();
my $oldfile = $this_dir . "/" . "test.doc";
my $newfile = $this_dir . "/" . "test2.doc";
my $oldtext = 'Hello';
my $newtext = 'olleH';
my $word =
Win32::OLE->GetActiveObject('Word.Application')
|| Win32::OLE-> new('Word.Application','Quit');
my $doc = $word-> Documents->Open("$oldfile");
# is application visible 0=no 1=yes
$word-> {visible} = 0;
my $search = $doc-> Content->Find;
my $replace = $search-> Replacement;
$search-> {Text} = $oldtext;
$replace-> {Text} = $newtext;
$search-> Execute({Replace => wdReplaceAll});
# save word file
$word-> ActiveDocument->SaveAs($newfile);
# close word file
$doc-> Close();
$word-> Quit();
# replace source word document with updated document
move($newfile, $oldfile) or die "move failed $!";
(or help improve my existing script) to update a Microsoft Word
document.
The following script searches for some text in the body of a Microsoft
Word document and replaces it with some other text. It does not update
text appearing the header or footer of a Word document. If anyone can
help modify it update text in header and in footer, I would really
appreciate it.
Thank you,
Deepak
#!c:/perl/bin/perl
####!/usr/bin/perl -w
# FILE: WordOLE.pl
# PURPOSE: Search/Replace a string in a word document
use strict;
use File::Copy;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
use Cwd;
my $this_dir = cwd();
my $oldfile = $this_dir . "/" . "test.doc";
my $newfile = $this_dir . "/" . "test2.doc";
my $oldtext = 'Hello';
my $newtext = 'olleH';
my $word =
Win32::OLE->GetActiveObject('Word.Application')
|| Win32::OLE-> new('Word.Application','Quit');
my $doc = $word-> Documents->Open("$oldfile");
# is application visible 0=no 1=yes
$word-> {visible} = 0;
my $search = $doc-> Content->Find;
my $replace = $search-> Replacement;
$search-> {Text} = $oldtext;
$replace-> {Text} = $newtext;
$search-> Execute({Replace => wdReplaceAll});
# save word file
$word-> ActiveDocument->SaveAs($newfile);
# close word file
$doc-> Close();
$word-> Quit();
# replace source word document with updated document
move($newfile, $oldfile) or die "move failed $!";