vba 根据客户 ID 将多页 MS Access 报告拆分为单独的 pdf 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11498794/
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-11 16:53:24  来源:igfitidea点击:

Splitting a Multi-Page MS Access report into individual pdf files based on a Customer ID?

ms-accessvbaaccess-vba

提问by user1527994

I'm looking to split a multi-page access report up into individual pdf files based on a Customer ID and then saving those pdf files based on the Customer Name (or ID).

我希望根据客户 ID 将多页访问报告拆分为单独的 pdf 文件,然后根据客户名称(或 ID)保存这些 pdf 文件。

I've looked into 'printing'/'converting' the report into one massive pdf file and splitting it there, but some customer invoices span two+ pages, therefore splitting that one pdf file page-by-page doesn't work.

我已经研究过将报告“打印”/“转换”成一个巨大的 pdf 文件并在那里拆分,但一些客户发票跨越两页以上,因此逐页拆分一个 pdf 文件不起作用。

Any help would be greatly appreciated; if anyone needs more clarification on anything, don't hesitate to let me know.

任何帮助将不胜感激; 如果有人需要更多说明,请随时告诉我。

回答by HansUp

This can be easy with Access 2007 (see note below) or later.

使用 Access 2007(请参阅下面的注释)或更高版本可以轻松实现这一点。

Open your report using the WhereConditionparameter to limit the record source to a specific customer.

使用WhereCondition参数打开报表以将记录源限制为特定客户。

DoCmd.OpenReport "rptFoo", acViewPreview, , "Customer_ID = 1"

Then use OutputTo to save it as PDF with a file name you supply.

然后使用 OutputTo 将其保存为 PDF,并使用您提供的文件名。

DoCmd.OutputTo acOutputReport, "", acFormatPDF, "Spacely_Sprockets.pdf"

Close the report afterward.

之后关闭报告。

DoCmd.Close acReport, "rptFoo"

You could create a procedure which opens a recordset for Customer_IDand Customer_Namedata, then use those 3 commands with values from each row.

您可以创建打开一个记录的程序Customer_IDCustomer_Name数据,然后使用这些命令3从各行的值。

If your Access version is older than 2007, you'll have to tell us about the method you're using to create PDF files.

如果您的 Access 版本早于 2007,您必须告诉我们您用于创建 PDF 文件的方法。

Note: For Access 2007, Office Service Pack 2 provides Built-in Save As PDF/XPS support.

注意:对于 Access 2007,Office Service Pack 2 提供内置的另存为 PDF/XPS 支持

回答by Fionnuala

PDF printing is available with MS Access 2010, for 2007, you can install an add-on from Microsoft : Print, share, and protect files in the PDF and XPS file formats

MS Access 2010 支持 PDF 打印,对于 2007,您可以安装 Microsoft 的附加组件:打印、共享和保护 PDF 和 XPS 文件格式的文件

For versions prior to 2007, you can use :

对于 2007 之前的版本,您可以使用:

  • Stephen LeBan's ReportToPDFconsists of just two DLLs but only works with MS Access

  • CutePDFis free and easy to use.

  • PDFCreatoris also free and can be fully automated with VBA (though I have not used it lately, I believe this feature is still available)

  • Any number of paid-for PDF creators.

  • Stephen LeBan 的ReportToPDF 仅包含两个 DLL,但仅适用于 MS Access

  • CutePDF免费且易于使用。

  • PDFCreator也是免费的,可以用VBA完全自动化(虽然最近没用过,相信这个功能还是有的)

  • 任意数量的付费 PDF 创建者。

The WHERE argument for OpenReport has been available since the 2003 version(also OpenReport 2010)

OpenReport 的 WHERE 参数自2003 版本(也是OpenReport 2010)开始可用

To print a report for all customers for version 2003, 2007 and 2010, you can loop through the relevant file and

要为版本 2003、2007 和 2010 的所有客户打印报告,您可以循环浏览相关文件并

Dim rs AS DAO.Recordset
Set rs = CurrentDB.OpenRecordset("SELECT DISTINCT CustimerID FROM Invoices")

   Do While Not rs.EOF
      ''expression.OpenReport(ReportName, View, FilterName, 
      ''      WhereCondition, WindowMode) -- 2010 has OpenArgs

      DoCmd.OpenReport "Invoices",<..>,,"CustomerID=" & rs!CustomerID
      ''OutputTo or other relevant code

      rs.MoveNext
   Loop

Getting a PDF then depends on your version and the tools you have installed. If you are using PDFCreator or CutePDF, for example, you can use acViewNormalfor the view. PDFCreator set up will allow you to assign a file name in advance, for CutePDF, you will have to fill in a file name. For Access 2007 and 2010, you can use OutputTo, as has already been mentioned, so acViewPreviewis best.

获取 PDF 取决于您的版本和安装的工具。例如,如果您使用 PDFCreator 或 CutePDF,则可以使用acViewNormal视图。PDFCreator 设置将允许您预先指定文件名,对于CutePDF,您必须填写文件名。对于 Access 2007 和 2010,您可以使用OutputTo,正如已经提到的那样,所以acViewPreview最好。

To email a report for 2007 and 2010, you can use SendObject. For earlier versions, you will need quite a bit more code. The easiest option is probably to automate Outlook.

要通过电子邮件发送 2007 年和 2010 年的报告,您可以使用SendObject。对于早期版本,您将需要更多的代码。最简单的选择可能是自动化 Outlook。