【问题标题】:How do I use Perl to run a macro in an **already open** Excel workbook如何使用 Perl 在**已打开** Excel 工作簿中运行宏
【发布时间】:2012-10-19 03:08:59
【问题描述】:

我想使用 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;

但是我该如何操作一个打开的工作簿呢?

【问题讨论】:

    标签: perl ole excel


    【解决方案1】:

    使用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";
            }
    

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2015-06-02
      • 2012-09-12
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多