我是MS Access的新手。目标是从MS Access DB导出多个PDF文件。目前,我可以通过以下代码导出多个Excel文件。但是我希望将它导出为PDF,在表格(颜色,字体等)中添加少量格式并添加标题和放大器。页脚注释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
由于
答案 0 :(得分:1)
Lybren的解决方案是正确的方法。创建要导出的所有查询的报告,然后在VBA中使用Docmd.OutputTo方法将报告导出为PDF。下面是一个代码示例:
DoCmd.OutputTo acOutputReport, "MyReportToExport", acFormatPDF, "C:/Users/MyUserAccount/Documents/MyExportedPDF.pdf"
您也可以通过更改" acOutputReport"来导出查询。 to" acOutputQuery"但通过使用报告,您可以更好地控制格式化。
答案 1 :(得分:1)
如上所述,PDF是结构化的,固定格式的文档文件,而不是动态数据文件。请考虑以下例程:
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