EPeddler has asked for the wisdom of the Perl Monks concerning the following question:

Hi , i'm new to perl and its been couple of days since i ve stepped into this world of PERL. I'm actually trying to write a script which can open a MS word document, search for the particular pattern ,say a particular command "chown and chmod" and note the following details. 1) Number of occurences of a pattern , full line of string containing the pattern into a different file. 2) Also , copy down the previous two lines and succeeding two lines after the occurencde of pattern. I see, WIN32::OLE is the way to do this. But i am unable to find any resource which could explain WIN32::OLE in terms of WORD and how we can manipulate the read data. say by printing it another doc or printing it to STDOUT. Though there are are couple of notes about WIN32 OLE , they are mainly with respect to Excel and also not so descriptive for a beginner. It would be helpful if someone can point me in right direction or give a code with description , it would be really helpful.

Replies are listed 'Best First'.
Re: WIN32 and MS Word
by davies (Monsignor) on Jul 23, 2012 at 21:54 UTC

    I put Perl "win32::ole" word into DuckDuckGo and got plenty of sites. The first was http://www.adp-gmbh.ch/perl/word.html, which seems to have well documented code examples. Another technique is to record a macro in Word that does what you want and then translate the VBA to Perl. This can be very powerful, but do remember that the recorded VBA is unlikely to be well written.

    Regards,

    John Davies

Re: WIN32 and MS Word !
by bulk88 (Priest) on Jul 23, 2012 at 22:47 UTC
    You can try searching PerlMonks (use super search to search inside posts, not the box you see in upper left corner on every page, that is titles only). Then Google. At the end of the day Win32::OLE can ONLY do what VB can do, nothing more (Win32::OLE only supports VB style IDispatch interfaces, not IUnknown (without IDispatch) C++/C interfaces). WithEvents is buggy in Win32::OLE, see rt://43574. Anything you can do in VB (not C++/C) you can do in Win32::OLE. If you want to know every possible method in the world with a OLE control, use oleview.exe, here is a screenshot http://www.sapnet.ru/examples/oleview_typelib.gif. Ultimately, the info you see with MSDN, oleview, plus the IDispatch-only are the only limitations you have with Win32::OLE. What will hold you (or me) back probably are poor knowledge of VB (so its hard port VB code to Perl with Win32::OLE since you know Perl, and Win32::OLE, but can't read VB), and MSDN's dry, terse and confusing docs, and not being able to figure out how to create the objects you want to use, and that you see in oleview, in VB or Perl, because you (often) cant create them from scratch (must come from a parent class/object), and the method that generates that object instance has an 8 word description on MSDN.
Re: WIN32 and MS Word !
by Anonymous Monk on Jul 24, 2012 at 08:09 UTC