Hi Peter,

This is a translation of a script from technet that works:

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

#
http://www.microsoft.com/technet/scriptcenter/resources/officetips/may05
/tips0512.mspx
       
  my $word = new Win32::OLE 'Word.Application','' or die "Cannot start
word!\n";
         
  $word->{visible}=1;
  
  my ( $file, $text ) = @ARGV;    
       
  my $doc = $word->Documents->Open( "$file" );

  $selection = $word->Selection;

  $selection->Find->{Text} = $text;
        
  $selection->Find->{Forward} = TRUE;
          
  $selection->Find->{MatchWholeWord} = TRUE;

  if ( $selection->Find->Execute ) {
    print "The search text was found\n";
  } else {       
    print "The search text was not found\n";
  }

Cheers,

Kev.

-----Original Message-----
Date: Sun, 12 Sep 2010 16:43:24 -0400
From: Peter Buck <pb...@his.com>
Subject: Win32::OLE on MS Word using Selection.Find.Execute
To: perl-win32-users@listserv.ActiveState.com
Message-ID: <4c8d3b6c.8010...@his.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

  Does anyone have an example of a perl script acting on MS Word
documents using Win32::OLE and Selection.Find.Execute?  I have read the
Win32 man page but its examples for Excel and Access don't help.  I 
found a Powershell script that does what I want but can't translate.   
The parts I'm confused on are (1) setting the parameters used in the
Selection.Find.Execute() invocation (particularly the boolean values,
since perl doesn't do booleans) and the actual invocation of
Selection.Find.Execute().

I did find an example using $search-> Execute() but this doesn't appear
to allow setting all the parameters that Selection.Find.Execute() does.

Also, it operates on $doc-> Content->Find while Selection.Find.Execute()
operates on $doc->Selection (if I'm right).  And I'm using Homekey(6) to
take me to the top of the document, which is linked to Selection and
doesn't seem to work in my $doc->Content->Find attempts.

Any help or direction to documentation much appreciated.

Thanks - Toolsmith

# ExpandAcronyms.ps1
# read acronym list, expand acronyms in target MS Word document #
syntax: ExpandAcronyms wordDocument

function make-change {
     $FindText = $args[0]
     $FullText = $args[1]
     $ReplaceText = "$FullText ($FindText)"

     $ReplaceAll = 1
     $FindContinue = 1
     $MatchCase = $True
     $MatchWholeWord = $True
     $MatchWildcards = $False
     $MatchSoundsLike = $False
     $MatchAllWordForms = $False
     $Forward = $True
     $Wrap = $FindContinue        # don't want it wrapping, wish I knew 
what this meant
     $Format = $False

     $objWord.Selection.HomeKey(6) > Null
     $result = $objSelection.Find.Execute($FindText,$MatchCase,
         $MatchWholeWord,$MatchWildcards,$MatchSoundsLike,
         $MatchAllWordForms,$Forward,$Wrap,$Format,
         $ReplaceText,$ReplaceAll)
      if ( $result -eq $true ) {
          "$Findtext|$FullText"
     }

}

if ( $args.count -lt 1 ) {
     cd $env:temp
     $strWordFile = [string](resolve-path(Read-Host "Enter Path of Word
file to be processed")) } else {
     $strWordFile = [string](resolve-path($args[0])) }


$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($strWordFile)

$objSelection = $objWord.Selection

$d = get-content "d:/temp/acronyms.txt"        # read file of acronym | 
definition
foreach ( $l in $d ) {
     ($acro, $def) = $l.split('|')                        # build array 
of acronym, definition
     make-change $acro $def
}
"Finished"
$objWord.Selection.HomeKey(6) > Null
This e-mail (including any attachments) may be confidential and may contain 
legally privileged information. You should not disclose its contents to any 
other person. If you are not the intended recipient, please notify the sender 
immediately.

Whilst the Council has taken every reasonable precaution to minimise the risk 
of computer software viruses, it cannot accept liability for any damage which 
you may sustain as a result of such viruses. You should carry out your own 
virus checks before opening the e-mail(and/or any attachments).

Unless expressly stated otherwise, the contents of this e-mail represent only 
the views of the sender and does not impose any legal obligation upon the 
Council or commit the Council to any course of action.
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to