使用Win32::OLE在Access 2007中执行宏
目前,我正在尝试通过 Perl OLE 在 Microsoft Access 中执行宏,
我想知道如何正确调用来运行宏。我尝试过
1) $oDatabase -> DoCmd.RunMacro("Macro1");
2) $oDatabase ->; DoCmd-> RunMacro("Macro1");
但他们向我抛出“无法在未定义的值上调用方法“DoCmd””或“无用的连接”
这是否可以通过 Win::32 OLE 执行 DoCmd ?任何帮助将不胜感激。
这是完整的代码。它尝试查找当前打开的 Microsoft Access。
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");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Microsoft 相当混乱的文档,DoCmd 是 Application 对象的属性,并且 RunMacro 是 DoCmd 的一个方法。在 Win32::OLE 中,方法使用方法语法,属性使用哈希语法。 (点“.”是 Visual Basic 语法。在 Perl 5 中,使用“->”)。
所以你的代码的最后两行应该是(我认为):
我没有 Access 2007,所以我无法测试它。
请注意, OpenCurrentDatabase 不会返回任何内容,这就是您的原因当您尝试调用 $oDatabase(未定义)上的方法时,出现“无法对未定义的值调用方法“DoCmd””。
指向 Microsoft 文档的链接在 2009 年 8 月 23 日有效,但 Microsoft 从未阅读过 Cool URI不会改变,因此您的里程可能会有所不同。
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):
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.
正如 HansUp 所说,您应该使用 Access 的 Application 实例变量来使用 DoCmd。
在你的情况下,它将翻译为
注意:我不知道 Perl :)
As HansUp said, you should use Access's Application instance variable to use DoCmd.
In your case, it will translate to
Note: I don't know Perl :)