Win32::OLE - help request - update MS Word document

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 $!";
 
A

A. Sinan Unur

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.

Here is something to get you started. Note that the code below is rather
repetitive and should probably be refactored. Also, if headers/footers
contain multiple paragraphs, then you should loop over each paragraph in
$header->{Range}->{Paragraphs} etc.

Use the object browser (accessible from the Visual Basic Editor in Word)
for details on properties and methods.

#!/usr/bin/perl

use strict;
use warnings;

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 2;

use FindBin qw( $Bin );
use File::Spec::Functions qw( canonpath catfile );

my $word = get_app('Word');
my $doc = $word->Documents->Open(canonpath(catfile $Bin, 'test.doc'));

my $sections = $doc->{Sections};
my $n_sections = $sections->{Count};

for my $s ( 1 .. $n_sections ) {
my $section = $sections->Item($s);
my $headers = $section->{Headers};
my $n_headers = $headers->{Count};
for my $h ( 1 .. $n_headers ) {
my $header = $headers->Item($h);
print $header->{Range}->{Text}, "\n";
}
my $footers = $section->{Footers};
my $n_footers = $footers->{Count};
for my $f ( 1 .. $n_footers ) {
my $footer = $footers->Item($f);
print $footer->{Range}->{Text}, "\n";
}
}

sub get_app {
my ($name) = shift;
my $app;
eval {
$app = Win32::OLE->GetActiveObject("$name.Application");
};

die "$@\n" if $@;

return $app if defined $app;

$app = Win32::OLE->new("$name.Application", sub { $_[0]->Quit })
or die "Oops, cannot start $name: ", Win32::OLE->LastError,
"\n";
}

D:\Home\asu1\UseNet\clpmisc\word> h
03/02/2006 09:29:53 PM


Header/Footer Example

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top