views:

311

answers:

3

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");
A: 

According to Access' Help, DoCmd is a property of the Application object. Your $oDatabase appears to be a database object. Maybe try DoCmd with your $oAccess object instead, but I have no idea what the syntax would be from Perl.

HansUp
A: 

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 :)

shahkalpesh
When down-voting, please explain the reason. Thanks :)
shahkalpesh
I wasn't the voter, but my guess would be it was down-voted because that's not the correct syntax for doing that in Perl.
cjm
Thanks cjm. I took it from the OP's question and I said that I don't know PERL :)
shahkalpesh
Unfortunately, half his problem is that he's gotten the syntax wrong. :-)
cjm
+1  A: 

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.

cjm