vba 如何以 PDF 格式输出报告,其中名称由字段中的值组成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2296884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:18:32  来源:igfitidea点击:

How can I output a report in PDF format, where the name consists of values from fields?

vbams-accessreportsave-as

提问by Zaid

I want to add functionality to my Access 2007 report whereby a PDF copy of the report is created at the click of a button. I know that there is an OutputTomacro which can do this for me, but it does not allow me to include report field values as part of the PDF's filename, namely:

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

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

While I have seen this MSDN threadand this SO question, I don't see the use of field values in any of the answers.

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

I reckon VBA code is the way to go, so I (unsuccessfully) tried the following:

我认为 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

Run-time error '2465':

Microsoft Office Access can't find the field '|' referred to in your expression

运行时错误“2465”:

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

Any ideas out there?

有什么想法吗?

回答by Zaid

I got it to work (eventually).

我让它工作(最终)。

The following subdid the trick:

以下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

Two caveats:

两个注意事项:

  1. The report needs to be opened before printing.
  2. Refer to fields by the same name that the report sees it as. That [Client Organisations].Codewas [Client Organisations_Code]in the report.
  1. 报告需要在打印前打开。
  2. 引用与报告相同名称的字段。那[Client Organisations].Code[Client Organisations_Code]在报告中。

回答by JonH

Its easier to Dim a string and put your entire expression there and then debug it to see if it contains the valid report name.

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

Like this:

像这样:

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.