Home > Blockchain >  DoCmd.OutputTo using PowerShell - 'wrong data type' for first argument
DoCmd.OutputTo using PowerShell - 'wrong data type' for first argument

Time:04-19

I'm busy migrating a few automated processes in MS Access from VBA to PowerShell. My VBA is okay but am new to PowerShell and seem to be missing something.

Using PowerShell I've got some variables going for file names etc. and the database is open. I've run a query and run the desired report in acViewReport view. In VBA my next line is as follows:

DoCmd.OutputTo acOutputReport, "RMyReport", "PDFFormat(*.pdf)", sFilepath, False, "", , acExportQualityPrint

Which works perfectly well in VBA. As far as I can tell, the PowerShell equivalent should be:

$Access.DoCmd.OutputTo([acOutputObjectType]::acOutputReport, "RMyReport", "PDFFormat(*.pdf)", $filepath, 0, "","", 0)

I have included the required enums for the relevant args in the following format:

enum acView {
    acViewNormal        # 0 (Default) Normal view
    acViewDesign        # 1 Design view
    acViewPreview       # 2 Print Preview
    acViewPivotTable    # 3 PivotTable view
    acViewPivotChart    # 4 PivotChart view
    acViewReport        # 5 Report view
    acViewLayout        # 6 Layout view
}
enum acOpenDataMode {
    acAdd       # 0     The user can add new records but can't view or edit existing records.
    acEdit      # 1     The user can view or edit existing records and add new records.
    acReadOnly  # 2     The user can only view records.
}
enum acOutputObjectType {
    acOutputTable           # 0     Table
    acOutputQuery           # 1     Query
    acOutputForm            # 2     Form
    acOutputReport          # 3     Report
    acOutputModule          # 5     Module
    acOutputServerView      # 7     Server View
    acOutputStoredProcedure # 9     Stored Procedure
    acOutputFunction        # 10    User-Defined Function
}

But get the following error:

An expression you entered is the wrong data type for one of the arguments.
At line:1 char:1
  $Access.DoCmd.OutputTo([acOutputObjectType]::acOutputReport, "RMyRepo ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : OperationStopped: (:) [], COMException
      FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Somehow I'm passing the incorrect datatype for arg1 but I assumed using the enumeration from the ms documentation would keep me on the right path.

In attempts to resolve, I've tried the following variations to no avail:

$Access.DoCmd.OutputTo(acOutputReport, "RMyReport", "PDFFormat(*.pdf)", $filepath, 0, "","", 0)
$Access.DoCmd.OutputTo(3, "RMyReport", "PDFFormat(*.pdf)", $filepath, 0, "","", 0)
$Access.DoCmd.OutputTo(acOutputReport, "", "PDFFormat(*.pdf)", $filepath, 0, "","", 0)
$Access.DoCmd.OutputTo(3, "", "PDFFormat(*.pdf)", $filepath, 0, "","", 0)

Any advice on how to adjust my OutputTo method would be a huge help, I'm completely stumped.

CodePudding user response:

Encoding is a Variant, not an empty string, so try leaving out the last unused parameters:

$Access.DoCmd.OutputTo([acOutputObjectType]::acOutputReport, "RMyReport", "PDFFormat(*.pdf)", $filepath)
  • Related