问题描述
我尝试为 Access 中的一个按钮创建一个代码,该按钮会打开“另存为”对话框,以使用预定义的文件名保存报告,并带有过滤器到 PDF 文件类型,用户可以在其中手动选择所需的文件夹。
>我还使用了下面的代码来创建一个不同的按钮,以将其保存为 Excel 文件类型,这可行,但我无法将其用于 PDF。
你们能帮帮我吗?谢谢。
Private Sub PrintButton_Click()
CurrentTime = Format(Now(),"DD_MM_YYYY hh:mm")
cmdlgOpenFile.filename = "Stock overview " & MonthMFiling & " " & YearMFiling & " " & CurrentTime & ".pdf"
cmdlgOpenFile.Filter = "PDF Files (*.pdf)|*.pdf"
cmdlgOpenFile.FilterIndex = clngFilterIndexAll
cmdlgOpenFile.DialogTitle = "Save Report As"
'this is where the dialog opens
cmdlgOpenFile.ShowSave
'returns your full file name.
filename = cmdlgOpenFile.filename
End Sub
解决方法
您的代码获取文件名。要将报告输出为 PDF,请使用以下命令:
DoCmd.OutputTo acOutputReport,"ReportName",acFormatPDF,filename
将“ReportName”替换为您正在使用的报告的名称。
文档:https://docs.microsoft.com/en-us/office/vba/api/Access.DoCmd.OutputTo