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');
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
What are you using to control MS Word? What Perl modules? Mind showing us your code? | [reply] |
Rewrite your program so it isn't bothered by copies of word/excel it didn't start. | [reply] |
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?
| [reply] |
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
| [reply] |
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 | [reply] [d/l] |