我正忙著將 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 方法的建議都會有很大的幫助,我完全被難住了。
uj5u.com熱心網友回復:
Encoding是一個 Variant,而不是一個空字串,因此請嘗試省略最后未使用的引數:
$Access.DoCmd.OutputTo([acOutputObjectType]::acOutputReport, "RMyReport", "PDFFormat(*.pdf)", $filepath)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459696.html
下一篇:將任意值插入自動編號列
