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

Discussion in 'Perl Misc' started by deepakp, Mar 3, 2006.

  1. deepakp

    deepakp Guest

    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 $!";
    deepakp, Mar 3, 2006
    #1
    1. Advertising

  2. "deepakp" <> wrote in
    news::

    > 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Drew Pihera
    Replies:
    0
    Views:
    641
    Drew Pihera
    Feb 4, 2004
  2. Lance Hoffmeyer
    Replies:
    0
    Views:
    238
    Lance Hoffmeyer
    Nov 17, 2003
  3. Tom Welch
    Replies:
    0
    Views:
    176
    Tom Welch
    May 26, 2004
  4. mmm ppp
    Replies:
    0
    Views:
    153
    mmm ppp
    Oct 17, 2005
  5. Lance Hoffmeyer

    Win32::OLE MS Word line breaks regex

    Lance Hoffmeyer, Feb 3, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    178
    Mumia W. (NOSPAM)
    Feb 4, 2007
Loading...

Share This Page