从MS Access导出为格式化的PDF [英] Export to formatted PDF from MS Access

查看:95
本文介绍了从MS Access导出为格式化的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MS Access非常陌生.目的是从MS Access DB导出多个PDF文件.目前,我可以通过以下代码导出多个Excel文件.但我想将其导出为PDF,并且在表格中进行一些格式更改(颜色,字体等),并添加页眉& PDF的页脚注释.

I am very new to MS Access. The objective is to export multiple PDF files from MS Access DB. Currently I can export multiple Excel files through the following code. But I want to export it to PDF with few format changes in table (Colors, Font etc) and adding Header & Footer notes to the PDF.

Private Sub Command4_Click()
Dim rsGroup As DAO.Recordset
Dim ColumnName As String
Dim myPath As String

myPath = "E:\TestExport\"

Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT TableName.Column FROM TableName", dbOpenDynaset)

Do While Not rsGroup.EOF
    ColumnName = rsGroup!Column

    Dim rsExportSQL As String
    rsExportSQL = "SELECT * FROM TableName WHERE TableName.Column='" & ColumnName & "'"

    Dim rsExport As DAO.QueryDef
    Set rsExport = CurrentDb.CreateQueryDef("myExportQueryDef", rsExportSQL)


    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "myExportQueryDef", myPath & RegionName & ".xls", True

    CurrentDb.QueryDefs.Delete rsExport.Name

    rsGroup.MoveNext
Loop

End Sub

谢谢

推荐答案

如上所述,PDF是结构化,固定格式的文档文件,而不是动态数据文件.考虑以下例程:

As mentioned, PDFs are structured, fixed formatted document files and not dynamic data files. Consider the following routine:

  1. 使用完整表格(而不是任何查询)将报表预先设计为 记录源.以任何方式(即风景/肖像,标题,字体,颜色)对其进行样式设置,以最终传播或展示.
  2. 使用 DoCmd.OpenReport 方法,打开报告在打印预览"中,使用方法的 WhereCondition 参数有条件地按循环值更改报告的过滤器.
  3. 然后运行 DoCmd.OutputTo 以输出预览在同一循环中以PDF格式报告.注意:完整表永远不会输出,只会过滤记录.
  1. Pre-design a report using the full table (not any query) as a recordsource. Style it in any way (i.e., landscape/portrait, headers, fonts, colors) for final dissemination or presentation.
  2. Using the DoCmd.OpenReport method, open the report in Print Preview, conditionally changing report's filter by loop value using the WhereCondition argument of the method.
  3. Then run DoCmd.OutputTo to output the previewed report to PDF format in same loop. NOTE: Full table is never outputted, only filtered records.

VBA 代码

Private Sub Command4_Click()
  Dim rsGroup As DAO.Recordset
  Dim ColumnName As String, myPath As String

  myPath = "E:\TestExport\"

  Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT Column FROM TableName", _
                                        dbOpenDynaset)        
  Do While Not rsGroup.EOF
     ColumnName = rsGroup!Column

     ' OPEN REPORT, FILTERING RECORDSOURCE BY COLUMN VALUE
     DoCmd.OpenReport "ReportName", acViewPreview, , "Column='" & ColumnName & "'"
     ' OUTPUT REPORT TO FILE
     DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, _
                                    myPath & ColumnName & ".pdf", False
     ' CLOSE PREVIEW
     DoCmd.Close acReport, "ReportName"

     rsGroup.MoveNext
  Loop

  rsGroup.Close
  Set rsGroup = Nothing
End Sub

这篇关于从MS Access导出为格式化的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆