如何从Perl或命令行激活Excel加载项?

(请原谅我对Excel加载项的无知,并在适当的地方随时纠正我的teminology。)

我有一个定期使用的Excel加载项。 此加载项插入一个工具栏与一些button。 我想自动完成在Excel中打开电子表格的任务,然后“单击”其中一个button。 换句话说,我想使用Perl(或命令行)来激活此加载项的特定function。

我无法立即访问加载项的源代码,但是我可以根据需要请求特定的信息,如过程名称。

我不能使用CPAN模块来完成这个任务 – 只有与我的ActivePerl版本一起安装 – 但是我有Win32 :: OLE ,这对其他Office自动化有帮助。

任何指针?

是否有工具栏button的键绑定?

如果有,您可以使用SendKeys方法将该密钥发送到Excel: http : //msdn.microsoft.com/en-us/library/aa202943(office.10).aspx

或者, CommandBars集合可能会有用。 请参阅http://msdn.microsoft.com/en-us/library/aa171356(office.11​​).aspx以供参考。

下面的示例代码列出了“标准”工具栏中的可见命令栏和控件。 当它find一个标题为Open的控件时,它调用该控件。 这应该显示“文件 – >打开”对话框:

#!/usr/bin/perl use strict; use warnings; use Win32::OLE qw(in with); $Win32::OLE::Warn = 3; my $app = get_excel(); $app->{Visible} = 1; my $book = $app->Workbooks->Add; for my $bar (in $app->CommandBars) { if ( $bar->{Visible} ) { print $bar->{Name}, "\n"; } } print "Controls in the 'Standard' toolbar:\n"; my $bar = $app->CommandBars->{Standard}; for my $control (in $bar->{Controls}) { print "\t", $control->{Caption}, "\n"; if ( $control->{Caption} =~ /^Open/ ) { print "opening ...\n"; $control->Execute; } } sub get_excel { my $excel; eval { $excel = Win32::OLE->GetActiveObject('Excel.Application'); }; die "$@\n" if $@; unless(defined $excel) { $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit }) or die "Oops, cannot start Excel: ", Win32::OLE->LastError, "\n"; } return $excel; } __END__ 

HTH

我不知道如何去点击其中一个button。
但我可能有一个解决方法。 如果你可以在excel中创build一个macros来按下从perl调用macros的button是可能的。

未经testing!

 #!c:\perl\bin\ use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Win32::OLE::Warn = 3; # Die on Errors. my $excelfile = $path_to_exelfile_with_macro; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($excelfile); $Excel->Run($MacroName); 

更多提示http://www.perlmonks.org/?node_id=153486

我认为你在Win32 :: OLE的正确轨道上。 过去,我使用它来自动化使用Excel。 我不相信OLE让你访问GUI元素(例如激活一个button按下),所以你可能需要找出工具栏是否支持OLE以及界面是什么。

我能想到的另一种方法是尝试以编程方式控制鼠标实际上单击button。 这假定你运行的OLE与Excel可见(它不一定是),并创build了解决如何定位光标的困难情况。 如果是一个选项,OLE会简单得多。