VBA - 我正在尝试将报告另存为 PDF,但它只能打开报告

VBA - I'm trying to save a report as a PDF, but it only opens the report

我的代码如下。它打开了报告,但看起来它就停在那里了。它永远不会到达消息框。所以我猜它在出口部分失败了。但是当我单击我的按钮时,我的断点不会触发。所以我看不到发生了什么。

有什么想法吗?

Private Sub Command552_Click()
    DoCmd.OpenReport "report_name", acViewPreview, , "[RowID] = 16094"
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, "C:\Users\name\Desktop\test.pdf", True

    DoCmd.Close acReport, "report_name"

    MsgBox ("done")
End Sub

由于您不确定代码哪里出了问题,我建议您从

中删除所有不必要的参数
DoCmd.OutputTo acOutputReport, "", acFormatPDF, "C:\Users\name\Desktop\test.pdf", True

所以你剩下这样的东西

DoCmd.OutputTo acOutputReport

系统会提示您输入文件名和位置,并选择文件格式。看看它是否正确保存。然后,将第一个参数放回代码行:

DoCmd.OutputTo acOutputReport, ""

重复该过程直到代码中断,然后您就缩小了崩溃的范围。

从那里,可以更容易地提供帮助。

您似乎想从表单中打开报表并输出它。

表格不知道你要输出报表。 因此,您要么必须将 doCmd.Output... 添加到报告中的代码,要么必须将指定对象设置为 doCmd.Output...

你可以试试:

DoCmd.OutputTo acOutputReport, "report_name", acFormatPDF, _ 
             "C:\Users\name\Desktop\test.pdf", True

问题出在我运行所在的服务器上。它没有支持导出为 PDF 的必要组件。当我将导出格式更改为 RTF 时,它工作正常。