Using Win32::OLE to execute macro in Access 2007

不问归期 提交于 2019-12-25 01:49:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!