Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Windows; If (2003) MS Word or Excel are already executing?

by gibsonca (Beadle)
on Jun 02, 2010 at 19:20 UTC ( #842813=perlquestion: print w/replies, xml ) Need Help??

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

My perl script opens a (2003) MS word document and creates an Excel document. Everything works fine except when the user does not exit out of all MS Word and/or MS Excel documents, not related to the files that my perl script would be reading. This problem can hang the perl script, or at best ask the user if a read-only copy is OK to use? This confuses the user a bit, since the file is not currently open. Any ideas or suggestions welcomed.

  • Comment on Windows; If (2003) MS Word or Excel are already executing?

Replies are listed 'Best First'.
Re: Windows; If (2003) MS Word or Excel are already executing?
by tokpela (Chaplain) on Jun 03, 2010 at 03:19 UTC

    I think this node might help you.

    You need to try to get the active object before you try to start another Excel instance.

    my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');

Re: Windows; If (2003) MS Word or Excel are already executing?
by sflitman (Hermit) on Jun 02, 2010 at 23:36 UTC
    You can programmatically close windows if you know their handles. Here are two non-tested examples.
    use Win32::GuiTest qw/SendMessage/; use Win32::FindWindow; $window = find_window( basename => 'winword.exe' ); SendMessage($window->hwnd,WM_CLOSE,0,0);
    OR
    use Win32::CtrlGUI; my $window =Win32::CtrlGUI::wait_for_window(qr/Word/); $window->send_keys("!fx"); my $window =Win32::CtrlGUI::wait_for_window(qr/Excel/); $window->send_keys("!fx");
    HTH,
    SSF
Re: Windows; If (2003) MS Word or Excel are already executing?
by romandas (Pilgrim) on Jun 03, 2010 at 02:03 UTC
    What are you using to control MS Word? What Perl modules? Mind showing us your code?
Re: Windows; If (2003) MS Word or Excel are already executing?
by Anonymous Monk on Jun 02, 2010 at 22:23 UTC
    Rewrite your program so it isn't bothered by copies of word/excel it didn't start.

      Is it actually possible to run concurrent copies of Word or Excel?

      Or does attempting to start a new copy when one is already running, just bring the existing instance to the foreground and open another document there?

        I can't speak for Word, which seems to perform differently, but it's certainly possible - and frequently necessary for me - to run multiple instances of Excel.

        Regards,

        John Davies
Re: Windows; If (2003) MS Word or Excel are already executing?
by davies (Prior) on Jun 04, 2010 at 18:29 UTC
    As I said earlier in the thread, I don't really know Word. However, there are some issues I know about in Excel that may transfer to Word. When opening a second instance of Excel manually, it usually asks me about read only copies of files like personal.xls, which contains macros I want always available. However, it doesn't do this when I open it from Perl. You may find setting the DisplayAlerts (if there is an analogue in Word) property to False may help, or it may just be the way I have my computer configured. If so, it's a function of something I don't know about.

    The code below works for me, without caring about the Word file being open. It also closes Word when the Perl exits, but not the new instance of Excel. Clearly, you can't save the Excel file if a file of that name is currently open. Also, there's some strange trailing character that gets copied across. I haven't tried chomping it - that's not in the question in the exam paper I read :-).
    use strict; use warnings; use diagnostics; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; my $word = Win32::OLE->new('Word.Application'); my $doc = $word->Documents->Open({FileName => "C:\\Perl\\eg\\842813\\W +ord.doc", ReadOnly => 1}); my $row=0; my $col=1; for (1..$doc->Paragraphs->Count) { $row++; $wb->Sheets(1)->Cells($row, $col)->{Value} = $doc->Paragraphs($_)- +>Range->Text; }
    Regards,

    John Davies

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://842813]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2023-10-02 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?