views:

33

answers:

1

I have an access database and a report in that database. I am using the access.application comobject to gain access to access, i.e.

$db = New-Object -ComObject Access.Application
$db.OpenCurrentDatabase("foo.accdb")

This works fine. However, as suggested in another similar question I am unable to get the report out of the db by using the DoCmd.OutputTo method.

$db.DoCmd.OutputTo(3,"The_Report","acFormatPDF","C:\The_Report.PDF")

When I execute that above command, and error returns stating that:

Exception calling "OutputTo" with "4" argument(s): 'The format in which you are attempting to output the current object is not available."

I have ensured that the "save to pdf" add-on is installed. Beyond that, I am unable to figure out what is stopping this object from being output in the requested format. Am I missing something?

+2  A: 

acFormatPDF is a constant, so putting the constant name in quotes seems wrong. I tried without the quotes, but Powershell doesn't appear to recognize the constant. So I tried the string value for that constant, in quotes, and it worked.

I'm basically lost with this Powershell thing, but suggest you try:

$db.DoCmd.OutputTo(3,"The_Report","PDF Format (*.pdf)","C:\The_Report.PDF")
HansUp
This is because when automating with COM you can't use the constant names defined within the application you're automating unless you've got references to the type library that allow you access to the definitions of those constants. "PDF Format (*.pdf)" is the literal value stored in the constant acFormatPDF. BTW, I was surprised to discover this, as I'd never encountered a named Access constant that returned a string value (other than ones that are obvious string values, like vbNullString and vbCrLf, but those are VBA constants, not Access ones). I expected the constant to be an integer!
David-W-Fenton
PDF Format (*.pdf) looks like what you would see in the drop-down of "save as"...
MattUebel
This answer seems to get me farther. Now it is telling me that "Microsoft Office Access cannot find the object 'The_Report'" Would a report by any other name be as hard to find?
MattUebel
+1 Works for me.
Remou
@Matt Shoot! Mine was rptFoo, and it worked. But like I said, I'm pretty much clueless about this Powershell. Not surprised there are other gotchas I didn't stumble across.
HansUp
@David I was also surprised the constant wasn't an integer. I'd even say stunned. :-)
HansUp
Thanks Hans... I created a new DB with a new test report and was able to get it to work. Must be something with my original DB, but at least I got the syntax right now. Weird non-integer constant and all hah
MattUebel