vba 如何向 Access 报告添加按钮以将其导出为 Excel/PDF?

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

How can I add a button to an Access report to export it to Excel / PDF?

ms-accessvbareport

提问by silverkid

How can I add a button to a Microsoft Access report to export it to Excel / PDF?

如何向 Microsoft Access 报告添加按钮以将其导出为 Excel/PDF?

采纳答案by David-W-Fenton

Christian has suggested a command button on a form, but you could also create a toolbar for the report with a button on it that would export the report to Excel. But as Tony says, the results are going to be ugly.

Christian 建议在表单上添加一个命令按钮,但您也可以为报表创建一个工具栏,上面有一个按钮,可以将报表导出到 Excel。但正如托尼所说,结果会很糟糕。

I would say that more useful would be a button that exports the data displayed in the report to an Excel spreadsheet. Formatting wouldn't be as pretty, but it would be much more useful and manipulable. For that, you'd use DoCmd.TransferSpreadsheet and a saved Query as your export source (equivalent to the Recordsource of the report).

我会说更有用的是将报告中显示的数据导出到 Excel 电子表格的按钮。格式化不会那么漂亮,但它会更有用和可操作。为此,您将使用 DoCmd.TransferSpreadsheet 和保存的 Query 作为导出源(相当于报告的 Recordsource)。

回答by user1204214

I just combined some of the previous answers and this is my final code block that exports a report to excel and then opens said excel file.

我只是结合了之前的一些答案,这是我的最终代码块,它将报告导出到 excel,然后打开所述 excel 文件。

Private Sub Command79_Click()
'initialize variables
Dim strReportName As String
Dim strPathUser As String
Dim strFilePath As String

'set variables
strReportName = "AlarmLetterForSF"
strPathUser = Environ$("USERPROFILE") & "\my documents\"
strFilePath = strPathUser & strReportName & Format(Date, "yyyymmdd") & ".xls"

'export to excel
DoCmd.OutputTo acOutputReport, strReportName, acFormatXLS, strFilePath

'launch excel file
Dim Shex As Object
Set Shex = CreateObject("Shell.Application")
Shex.Open (strFilePath)
End Sub

回答by Tony Toews

Modules: Sample Excel Automation- cell by cell which is slow

模块:示例 Excel 自动化- 逐个单元格缓慢

Modules: Transferring Records to Excel with Automation

模块:使用自动化将记录传输到 Excel

Note though the Excel automation suggested is actually against a query as exporting reports to Excel makes them exceedingly ugly. If I recall correctly this feature was removed in Access 2007.

请注意,尽管建议的 Excel 自动化实际上是针对查询的,因为将报告导出到 Excel 会使它们变得非常难看。如果我没记错的话,这个功能在 Access 2007 中被删除了。

A2000ReportToPDFis an Access 2000 database containing a function to convert Reports and Snapshot files to PDF documents. No PDF Printer driver is required. Free.

A2000ReportToPDF是一个 Access 2000 数据库,包含将报告和快照文件转换为 PDF 文档的功能。不需要 PDF 打印机驱动程序。自由。

回答by Christian Payne

AFAIK you can't "add" it to the report. But on the form that opens the report, you could add a button with the following code:

AFAIK 您不能将其“添加”到报告中。但是在打开报表的表单上,您可以添加一个带有以下代码的按钮:

DoCmd.OutputTo acOutputReport, "ReportName", acFormatXLS,
"c:\YourFolderName\ReportName - " & Format(Date, "yyyymmdd") & ".xls"

Only Access 2007 has support for PDF's. So you will either need to install a PDF printer or use Tony Toews suggestion

只有 Access 2007 支持 PDF 的. 因此,您要么需要安装 PDF 打印机,要么使用 Tony Toews 的建议