using Win32::OLE

Rob Manning r_m_manning at hotmail.com
Wed Dec 8 10:00:53 CST 1999


Sorry folks, this was a little too big for the mailing list
server's taste before so it bounced...  I'm going to break the
message into 3 parts to see if that will work.  The first
part will contain the body of the message.  The second part
will contain the example Word Document(ben_test.doc),
and the third the text file produced(ben_test.txt) by running the perl 
script on the Word doc.


	To follow up on last night's discussion, I'd like to
clarify a couple of things.  First, the Win32::OLE package
is a "standard extension" in the ActiveState Win32 port,
which simply means it is included in the Perl install.  It was
authored by Gurusamy Sarathy and Jan Dubois
(not Jenda Krynicky as I said last night). It replaces
the original OLE.pm module.

The book I mentioned last night that devotes a chapter to
using Automation(OLE) within Perl is called
"Win32 Perl Programming: The Standard Extensions"
by Dave Roth and published by MacmillanTechnical Publishing.
It gives some good background on how OLE came about and
also a number of examples on how to use it.  If you are stuck
using Win32 for developing/running Perl programs this book
is a tremendous resource.

There was some debate on how to find a string of text
embedded in a Word document.  Some suggested the
use of a "grep" type approach. Included is a
Word 97 Document that contains the String
"Account: 23423-34534555435"
on page 5 under the section header "Reporting Bugs".
Yet when you open it in a regular text editor to do a
string search for "Account"  there is no match.  In many
cases a string search _WILL_ yield desired results, although
heavy formatting reduces the probability that this will work.
(see the attached file ben_test.doc)

So, an OLE automation approach is probably the way to go.

Now I whipped up a start for the brute force approach (opening
a Word Document and  saving it as *.txt ) which could be
put into a loop with a regex to find the String "Account".
Here it is:

# This example was adapted from Dave Roth's
# book entitled "Win32 Perl Programming: The Standard Extensions"
# P. 176

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

$file = 'c:\tmp\ben_test.doc';
$new_file = 'c:\tmp\ben_test.txt';
$WordObj = Win32::OLE->new('Word.Application', 'Quit') or die "Couldn't
run Word";
$wd = $WordObj->Documents->Open($file);
$WordObj->ActiveDocument->SaveAs({
		FileName => $new_file,
		FileFormat =>wdFormatText,
	});
$wd->Close();
$WordObj->exit;

Note:  It is very important to use absolute paths to nail down the
location of the resultant file.  If you don't,  we found last year that
WinNT sticks it in the default user area, somoething like
c:\winnt\profiles\<user>\personal ...

The resultant file that was created by running the script
on the ben_test.doc file is attached as ben_test.txt

Now a string search finds the word Account and the actual
info next to it on the same line as it appeared in the Word
Document.

With a little more effort we could find the right object methods
to perform the find explicitly on the Word document object (wd)
which would eliminate the need for saving to text.

Rob

P.S.  BTW, I didn't make a new word last night, "forwent" is a real word ;-)

Rob Manning                                     manningr at tcsnet.net
Senior Systems Analyst                      Work (410) 396-4963
TeleCommunication Systems               Fax  (410) 837-0546






______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com



More information about the Baltimore-pm mailing list