如何使用Perl在**已经打开的Excel工作簿中运行宏 [英] How do I use Perl to run a macro in an **already open** Excel workbook

查看:128
本文介绍了如何使用Perl在**已经打开的Excel工作簿中运行宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Perl在已经打开的 Excel工作簿中运行宏.

I would like to use Perl to run a macro in an already open Excel workbook.

如果我只想打开工作簿并运行宏,则以下代码有效:

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?

推荐答案

使用GetActiveObject.来自文档:

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

这篇关于如何使用Perl在**已经打开的Excel工作簿中运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆