Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Search and replace in multple MS Word documents

by hominid (Priest)
on Sep 02, 2009 at 15:16 UTC ( [id://792942]=note: print w/replies, xml ) Need Help??


in reply to Search and replace in multple MS Word documents

This is a simple example of searching and replacing case sensitive text in a MS Word document. Use a loop to handle multiple docs.
use strict; use warnings; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 1; my $word = Win32::OLE->new("Word.Application"); $word->{DisplayAlerts} = 0; $word->{Visible} = 0; my $doc = $word->Documents->Open("\\YOUR\\PATH\\TO\\FILE"); my $selection = $doc->Content->Find; $selection->{Text} = "hippopotamus"; $selection->{Replacement}->{Text} = "elephant"; $selection->{MatchCase} = 1; $selection->Execute({Replace => wdReplaceAll}); $doc->SaveAs("\\YOUR\\PATH\\TO\\ALTEREDFILE"); $doc->Close; $word->Quit;

Replies are listed 'Best First'.
Re^2: Search and replace in multple MS Word documents
by Wobbel (Acolyte) on Sep 03, 2009 at 07:32 UTC
    Thanks a lot! Creating documents (xls, doc) is possible, but I was not sure if it was possible to manipulate excisting documents. Performance issues with a lot of documents? I'm very glad with the code (and it ain't VB ;-) ....)
      Performance issues with a lot of documents?
      Shouldn't be. But the specific answer depends on your performance requirements and your definition of "a lot". (358 files doesn't seem like a lot to me.) Just test and see.
      ...but I was not sure if it was possible to manipulate excisting documents.
      Corion's advice about macros is dead on. And after you have seen some of the macro code side by side with the equivalent Perl code, it gets easier to translate between the two. The Word Object Model Reference is a handy information source.
        s~handy~critical~;
        It's almost working. I have to use a forward slashes (Windows-enviroment...) and I want to use a substitution, due to weird Dutch spelling rules (single / plural). In stead of a fixed text string(?) like: $selection->{Text} = "hippopotamus"; something like: s/(laborant|laboranten)/(MBB-er|MBB-ers)/; (plural: in Dutch it's plus "en" or plus "s" ...) Any elegant suggestions?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://792942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-16 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found