在VBA中,我想打开一个报告,更改过滤器,然后将其输出到pdf文件。我将遍历一系列值,根据值更改过滤器并将每个值作为单独的文件输出。我的代码如下。虽然过滤器属性似乎随每次迭代而改变,但在添加rpt.Requery
行之前,输出文件都是相同的(就好像过滤器没有被更改一样)。因此,我添加了rpt.Requery
行,以尝试刷新报表中的数据。现在它给了我运行时错误2467,"你输入的表达式是指一个已关闭或不存在的对象。"我做错了什么?
Dim b As Scripting.Dictionary
Dim rptName As String
Dim rpt As Report
Dim pdfNamePrefix As String
Dim k As Variant
Dim v As String
Dim fileName As String
Set b = New Scripting.Dictionary
b(1) = "value1"
b(2) = "value2"
' ...
rptName = "SomeReport"
DoCmd.OpenReport rptName, acViewPreview
Set rpt = Reports(rptName)
pdfNamePrefix = "x"
For Each k In b.Keys
v = b(k)
fltr = "PType IN (""" & v & """)"
fileName = "some folder path\" & k & ".pdf"
rpt.Filter = fltr
' All good until the next line.
' This throws an error.
rpt.Requery
DoCmd.OutputTo acOutputReport, rpt.Name, acFormatPDF, fileName, False
Next k
DoCmd.Close acReport, rptName, acSaveNo