简体   繁体   English

如何使用Perl在**已经打开的Excel工作簿中运行宏

[英]How do I use Perl to run a macro in an **already open** Excel workbook

I would like to use Perl to run a macro in an already open Excel workbook. 我想使用Perl在已经打开的 Excel工作簿中运行宏。

The following code works if I just want to open a workbook and run a macro: 如果我只想打开工作簿并运行宏,则以下代码有效:

#!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;

But how do I operate on an open workbook? 但是,如何处理打开的工作簿?

Use GetActiveObject . 使用GetActiveObject From 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";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM