问题描述
当前,我正在尝试通过Perl OLE在Microsoft Access中执行宏
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");
1) $oDatabase -> DoCmd.RunMacro("Macro1");
2) $oDatabase -> DoCmd -> RunMacro("Macro1");
但是他们让我无法在未定义的值上调用方法"DoCmd""或对合并的无用使用"
But they throw me "Can't call method "DoCmd" on an undefined value" or "useless use of concatentation"
这是否有可能通过Win :: 32 OLE执行DoCmd?任何帮助将不胜感激.
Is this even possible to execute a DoCmd through Win::32 OLE? Any help would be greatly appreciated.
这是完整的代码.它尝试查找打开的当前Microsoft Access.
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");
推荐答案
根据 Microsoft令人困惑的文档, DoCmd 是属性"和 RunMacro 是DoCmd的方法.在Win32 :: OLE中,方法使用方法语法,而属性使用哈希语法. (点."是Visual Basic语法.在Perl 5中,使用->").
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");
我没有Access 2007,所以无法测试.
I don't have Access 2007 so I can't test this.
请注意, OpenCurrentDatabase 不返回任何内容,这就是为什么尝试在$ oDatabase(undef)上调用方法时,出现无法在未定义的值上调用方法"DoCmd".
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).
Microsoft文档的链接于2009年8月23日生效,但Microsoft从未阅读很酷的URI不会改变,因此您的里程可能会有所不同.
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.
这篇关于使用Win32 :: OLE在Access 2007中执行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!