How can I activate an Excel add-in from Perl or th

2020-06-22 03:46发布

(Please forgive my ignorance on Excel add-ins, and feel free to correct my teminology where appropriate.)

I have an Excel add-in that is used regularly. This add-in inserts a toolbar with a number of buttons. I want to automate the task of opening a spreadsheet in Excel and then "clicking" one of those buttons. In other words, I want to use Perl (or the command line) to activate a particular function of this add-in.

I do not have immediate access to the source code for the add-in, but I should be able to request specific information such as procedure names if required.

I cannot use CPAN modules for this task—only what is installed with my version of ActivePerl—but I do have Win32::OLE, which has been helpful for other Office automation.

Any pointers?

3条回答
爷、活的狠高调
2楼-- · 2020-06-22 04:27

Is there a key binding for the toolbar button?

If there is, you could use the SendKeys method to send that key to Excel: http://msdn.microsoft.com/en-us/library/aa202943(office.10).aspx

Alternatively, the CommandBars collection might be useful. See http://msdn.microsoft.com/en-us/library/aa171356(office.11).aspx for reference.

The sample code below lists the visible command bars and the controls in the 'Standard' toolbar. When it finds a control with the caption Open, it invokes the control. This should display the "File -> Open" dialog:

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

查看更多
孤傲高冷的网名
3楼-- · 2020-06-22 04:32

I don't know how you would go about clicking one of those buttons.
But I might have a workaround. If you can create a macro in excel to press the button calling that macro from perl is possible.

Untested!

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

More tips at http://www.perlmonks.org/?node_id=153486

查看更多
劳资没心,怎么记你
4楼-- · 2020-06-22 04:44

I think you're on the right track within Win32::OLE. I've used it to automate working with Excel in the past. I don't believe that OLE gives you access to GUI elements (e.g. activating a button press) so you'll probably need to find out whether or not the toolbar supports OLE and what the interface is.

The other alternative I can think of would be to try programmatically control the mouse to actually click the button. That assumes that you're running OLE with Excel visible (it doesn't have to be) and creates the difficult situation of figuring out how to position the cursor. OLE would be much simpler if it's an option.

查看更多
登录 后发表回答