我想使用Perl在已打开的 Excel工作簿中运行宏。
如果我只想打开工作簿并运行宏,则以下代码有效:
#!C:\Perl\bin\perl.exe
use strict;
use Win32::OLE;
my $Excel = Win32::OLE->new('Excel.Application') or die;
$Excel->Workbooks->open('M:\Programs\MyExcelFile.xls');
$Excel->run('Book1!ChartData');
$Excel->quit;
但我如何使用打开工作簿?
答案 0 :(得分:0)
使用GetActiveObject
。来自the docs:
Here is a simple Microsoft Excel application.
use Win32::OLE;
# use existing instance if Excel is already running
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $ex) {
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
}