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

Dear all!

I need to write a Perl application that manipulates MS Word to do something.

Perl starts MS Word application, performs some manipulations and allows for a user to edit in MS Word what he wants.

In order to do that, my perl program performs necesarry initialisations and goes to message loop by Win32::OLE->MessageLoop. After that my program could get and process many types of events, such as Quit, DocumentChange and so on.

But I can not find a way to send any kind of Event to my perl script programmatically, (I mean events come to my perl program as they occur, such as DocumentChange, but I can not send any other kind of event myself).

Does anyone knows a way how to send to my program any type of signal in my case?

Thanks in advance,
Courage, the Cowardly Dog

Replies are listed 'Best First'.
Re: Perl+MSWord (via Win32::OLE) events handling
by diotalevi (Canon) on Sep 26, 2002 at 12:39 UTC

    If you don't get an answer here try the ActiveState mailing list. For obvious reasons the majority of folks there are Windows people. (myself included)

Re: Perl+MSWord (via Win32::OLE) events handling
by MZSanford (Curate) on Sep 26, 2002 at 13:31 UTC
    Depending on what you want to make it do, it is possible the OLE interface has a method to do it. You can use something like OLESpy, or the OLE/COM Viewer tool that comes with Visual C++ (and i think elsewhere too) to verify. If there is no method, i do not believe there is a was to 'fire' and event, as events are supposed to signal something within the COM Object, and the external execution of that would be, well, icky (Technical Term®).
    from the frivolous to the serious
      Have you used the ActiveState OLE Browser? I've had to use Win32::OLE to automate some Word events en masse, but nothing this complicated (plus I cheated..I used a word template file and navigated around it using bookmarks), and when trying to RTFM on my own, I decided to use the browser. I thought it was overall pretty lousy, there was absolutely no documentation for the libraries, just class/method/property names. Are these tools much better? If so, I think it would be very useful to other monks using Win32.

      As for the original question, I'd agree..poke around the OLE library, also perhaps the Win32::API documentation, it would be a whole lot messier, but if you truly, absolutely, positively need it done, it may be the only way.
      Thank you for help,
      Actually event processing is quite okay in Win32::OLE module, although it is in alpha state. (as perldoc Win32::OLE says), so I was dreaming about a possibility to send not only standard event, but some kind of my own notification.

      One way exists to solve my task for sure, but it is too complicated, and I am afraid of implementing it.
      Idea is to use PerlScript engine from MS-Word to communicate my initial script via, say, "PostThreadMessage" or alike. (PerlScript from ActiveState was mentioned somewhere at this site)
      but I really afraid of such complications: there should be too much conditions to be met in order to run a program, one of them - perl-5.8.0 will not be able to do that currently, as it is not PerlScript-aware, AFAIK.

      Courage, the Cowardly Dog

Re: Perl+MSWord (via Win32::OLE) events handling
by rob_au (Abbot) on Sep 26, 2002 at 13:55 UTC
    While not a direct answer to your question, there was an excellent node on creating and manipulating Microsoft Word documents via OLE with Perl a few weeks ago from Kenny - This node provides a very good example of how to interact with Microsoft Word from Perl and should form a very good starting point as to the type of information readily available via the OLE interface.

     

Re: Perl+MSWord (via Win32::OLE) events handling
by t0mas (Priest) on Sep 26, 2002 at 13:50 UTC
    Do you by asking:
    Does anyone knows a way how to send to my program any type of signal in my case?
    mean that you want to send a message to Word, that gets processed by the message loop in your perl program?

    If so, you could try:
    #!/usr/bin/perl -w use Win32::API; my $find = new Win32::API("user32", "FindWindowA", 'PN', 'N') or die "No find"; my $send = new Win32::API("user32", "SendMessageA", 'NNNN', 'N') or die "No send"; # Find any Word window my $wordhandle=$find->Call("OpusApp",0); print "Handle: ",$wordhandle,"\n"; $send->Call($wordhandle, 0x0010, 1, 0) if ($wordhandle);
    This should close Word and generate a "Quit" event.


    /brother t0mas
      In a few words, I meant following idea:

      I am able to communicate from Perl to MS-Word.

      I want to be able to do reverse communication as well, notify my perl script of my event, give it processor ticks

      Thank you for your help which contained interesting ideas
      Courage, the Cowardly Dog