use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; # set $filepath to the full path to the directory where the file to be changed, test.doc, is stored my $filepath = 'C:/tmp'; my $oldfile = $filepath . '/test.doc'; my $newfile = $filepath . '/test1.doc'; my ($oldtext, $newtext); my (%doll_str, $doll_item, $search_res, $replace_res, $exec_res); $doll_str{xx1} = 'fred 1'; $doll_str{xx2} = 'orange 2'; $doll_str{xx3} = 'green 3'; $doll_str{xx4} = 'blue 4'; $doll_str{xx5} = 'black 5'; 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}); foreach $doll_item (sort {$a cmp $b} keys %doll_str) { $search-> {Text} = $doll_item; $replace-> {Text} = $doll_str{$doll_item}; $exec_res = $search-> Execute({Replace => wdReplaceAll}); print "item <$doll_item> exec <$exec_res>/n"; } # save word file $word-> ActiveDocument->SaveAs($newfile); # close word file $doc-> Close(); $word-> Quit();