参照Access网友“宏鹏 ”的示例“报表导出为pdf文件”基础上进行了适当的修改,因为实际的需求是按类别将报表批量导出为pdf。
附 件:
点击下载此附件
代 码:
Private Sub btnpdf_Click()
On Error GoTo ErrorHandler
Dim strPath As String
With FileDialog(4)
.Title = "选择附件路径"
.AllowMultiSelect = False
If .Show Then
strPath = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Cars
DoCmd.OpenReport "rptProducts", acViewPreview, , "Category='Cars'", acHidden
DoCmd.OutputTo acOutputReport, "rptProducts", acFormatPDF, strPath & "\Cars_Products.pdf", False '导出PDF
DoCmd.Close acReport, "rptProducts"
'Minivans
DoCmd.OpenReport "rptProducts", acViewPreview, , "Category='Minivans'", acHidden
DoCmd.OutputTo acOutputReport, "rptProducts", acFormatPDF, strPath & "\Minivans_Products.pdf", False '导出PDF
DoCmd.Close acReport, "rptProducts"
'SUV
DoCmd.OpenReport "rptProducts", acViewPreview, , "Category='SUV'", acHidden
DoCmd.OutputTo acOutputReport, "rptProducts", acFormatPDF, strPath & "\SUV_Products.pdf", False '导出PDF
DoCmd.Close acReport, "rptProducts"
'Trucks
DoCmd.OpenReport "rptProducts", acViewPreview, , "Category='Trucks'", acHidden
DoCmd.OutputTo acOutputReport, "rptProducts", acFormatPDF, strPath & "\Trucks_Products.pdf", False '导出PDF
DoCmd.Close acReport, "rptProducts"
MsgBox "导出成功。", vbInformation
Shell "explorer " & strPath, vbNormalFocus
ExitHere:
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbCritical
Resume ExitHere
End Sub