ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
RE: Microsoft Word search and replace issue
by Chris Cox other posts by this author
May 19 2005 9:53PM messages near this date
view in the new Beta List Site
Re: Server memory stats | RE: Microsoft Word search and replace issue
Hi all,

Further on this, I found I had to loop over all the Shapes in the Shapes collection. eg:

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

my $word = Win32::OLE-> new('Word.Application');
$word-> {visible} = 1;

my $doc = $word-> {Documents}->Open("C:\\Word\\Certificate.doc");
my $count = $doc-> Shapes->{Count};
for (my $i=1; $i < $count; $i++) {
	$doc-> Shapes->{$i}->{TextFrame}->{TextRange}->{Text} =~ s/#tPreferredName#/Chris/g;
}

Surely there's an easier way? If not, it'll have to do!

Chris

----------------------------------------------
Chris Cox
B.IT.(Information Systems) 
Senior Software Engineer

Creatop Interactive Media
Level 1, 240 McCullough Street
Sunnybank. Qld. 4109
Australia
Ph:      1300 85 80 85
Int'l Ph:     +61 7 3216 9755
Mobile: +61 412 416600
http://www.creatop.com.au/
chris.cox@[...].au
----------------------------------------------

----- Original Message ----- 
From: chris.cox@[...].au
To: perl-win32-users@[...].com
Sent: 
Subject: Microsoft Word search and replace issue Hi all,

Firstly, I hope sending an attachment is allowed. I figure it'll be easier to provide a real
 example of my problem.

Anyway, I'm trying to write a basic perl script to open a Word document, change some predefi
ned values to something else.

It works fine on a basic document, but anything with text boxes it doesn't work at all.

The script I run is:

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

my $word = Win32::OLE-> new('Word.Application');
$word-> {visible} = 1;
my $doc = $word-> {Documents}->Open("C:WordCertificate.doc");

my $search =  $doc-> Content->Find;
my $replace = $search-> Replacement;
$search-> {Wrap} = wdFindContinue;
$search-> {Text} = "#tPreferredName#";
$replace-> {Text} = "Chris";
$search-> Execute({Replace => wdReplaceAll});

$doc-> SaveAs("C:WordCertificate_changed.doc");

I've attached a ZIP file with the code, and 2 different styles of Word Documents. One, a bas
ic one with hardly anything, and 2 the actual document we need to work with.

Any help would be appreciated, I'm new at this!

Cheers,

Chris

----------------------------------------------
Chris Cox
B.IT.(Information Systems) 
Senior Software Engineer

Creatop Interactive Media
Level 1, 240 McCullough Street
Sunnybank. Qld. 4109
Australia
Ph:      1300 85 80 85
Int'l Ph:     +61 7 3216 9755
Mobile: +61 412 416600
http://www.creatop.com.au/
chris.cox@[...].au
---------------------------------------------- 
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Chris Cox
Jan Dubois
Greg Chapman

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved