问题
Currently I am trying to execute a macro in Microsoft Access through Perl OLE
I am wondering how to properly make the call to run a macro. I have tried
1) $oDatabase -> DoCmd.RunMacro("Macro1");
2) $oDatabase -> DoCmd -> RunMacro("Macro1");
But they throw me "Can't call method "DoCmd" on an undefined value" or "useless use of concatentation"
Is this even possible to execute a DoCmd through Win::32 OLE? Any help would be greatly appreciated.
Here is a complete code. It tries to look for the current Microsoft Access that is opened.
use strict;
use warnings;
use Win32::OLE;
my $oAccess;
my $oDatabase;
my $filename = "C:\\Sample.accdb";
$oAccess = Win32::OLE->GetActiveObject('Access.Application');
$oDatabase = $oAccess->OpenCurrentDatabase($filename);
$oDatabase -> DoCmd.RunMacro("Macro1");
回答1:
According to Microsoft's rather confusing documentation, DoCmd is a property of the Application object, and RunMacro is a method of DoCmd. In Win32::OLE, methods use method syntax and properties use hash syntax. (The dot '.' is Visual Basic syntax. In Perl 5, use a '->').
So the last two lines of your code should be (I think):
$oAccess->OpenCurrentDatabase($filename);
$oAccess->{DoCmd}->RunMacro("Macro1");
I don't have Access 2007 so I can't test this.
Note that OpenCurrentDatabase does not return anything, which is why you're getting "Can't call method "DoCmd" on an undefined value" when you try to call methods on $oDatabase (which is undef).
Links to Microsoft's documentation worked on August 23, 2009, but Microsoft has never read Cool URIs don't change, so your mileage may vary.
回答2:
As HansUp said, you should use Access's Application instance variable to use DoCmd.
In your case, it will translate to
$oAccess->DoCmd.RunMacro("macro1");
Note: I don't know Perl :)
来源:https://stackoverflow.com/questions/1314637/using-win32ole-to-execute-macro-in-access-2007