5

我想向我的 Access 2007 报告添加功能,通过单击按钮创建报告的 PDF 副本。我知道有一个OutputTo宏可以为我执行此操作,但它不允许我将报告字段值包含为 PDF 文件名的一部分,即:

[Client Organisations].Code + "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy") + ".pdf"

虽然我看过这个MSDN 线程和这个SO question,但我没有看到任何答案中使用了字段值。

我认为 VBA 代码是要走的路,所以我(不成功)尝试了以下方法:

Private Sub Create_PDF_Click()
DoCmd.OutputTo acOutputReport, , acFormatPDF, "" + [Client Organisations].Code  
+ "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy")
+ ".pdf", True
End Sub

运行时错误“2465”:

Microsoft Office Access 找不到字段“|” 在你的表达中提到

有什么想法吗?

4

2 回答 2

15

我让它工作(最终)。

以下sub是诀窍:

Private Sub Create_PDF_Click()

Dim myPath As String
Dim strReportName As String

DoCmd.OpenReport "Invoices", acViewPreview

myPath = "C:\Documents and Settings\"
strReportName = Report_Invoices.[Client Organisations_Code] + "-" +
Report_Invoices.Clients_Code + "-" + Report_Invoices.Invoices_Code + "-" +
Format(Report_Invoices.[Invoice Date], "yyyy") + ".pdf"

DoCmd.OutputTo acOutputReport, "", acFormatPDF, myPath + strReportName, True
DoCmd.Close acReport, "Invoices"

End Sub

两个警告:

  1. 报告需要在打印前打开。
  2. 引用与报告相同的名称的字段。那[Client Organisations].Code[Client Organisations_Code]在报告中。
于 2010-02-20T10:29:05.863 回答
1

更容易将字符串变暗并将整个表达式放在那里,然后对其进行调试以查看它是否包含有效的报告名称。

像这样:

Dim strReportName as String

strReportName = [Client Organisations].Code   
+ "-" + Clients.Code + "-" + Invoices_Code + "-" + Format([Invoice Date],"yyyy") 
+ ".pdf"

//then try to print strReportName before you use DoCmd.OutputTo.
于 2010-02-19T14:20:44.637 回答