Chris Cox wrote:

I'm trying to write a basic perl script to open a Word document, change some 
predefined 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:\\Word\\Certificate.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:\\Word\\Certificate_changed.doc");



I'm not sure what you mean by a text box, (an object with text?) Sometimes that easiest way of figuring out how to do something in Word (or Excel) is to record a macro inside Word, then look at and convert the VBA code to perl. In this case search and replace produces the following VBA macro in Word 2000:

   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
       .Text = "test"
       .Replacement.Text = "chris"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute Replace:=wdReplaceAll

Perhaps it's in one of the options that you didn't set in your code.
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to