DoCmd.OutputTo 使用 PowerShell - 'wrong data type' 作为第一个参数

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

我正忙于将 MS Access 中的一些自动化进程从 VBA 迁移到 PowerShell。我的 VBA 没问题,但我是 PowerShell 的新手,似乎遗漏了一些东西。

使用 PowerShell 我有一些用于文件名等的变量,并且数据库是打开的。我在 acViewReport 视图中 运行 查询和 运行 所需的报告。在 VBA 我的下一行如下:

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

在 VBA 中效果很好。据我所知,等效于 PowerShell 的应该是:

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

我已经按照以下格式包含了相关参数所需的枚举:

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
}

但是得到如下错误:

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

我以某种方式为 arg1 传递了不正确的数据类型,但我假设使用 ms 文档中的枚举会让我走上正确的道路。

为了解决问题,我尝试了以下变体但无济于事:

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

任何关于如何调整我的 OutputTo 方法的建议都将是一个巨大的帮助,我完全被难住了。

Encoding 是 Variant,不是空字符串,因此请尝试省略最后未使用的参数:

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