-
1
- #1
Here's the code for a function to print (or preview) a report. I've used this successfully in a number apps. I hope that the lines come out correctly here.
Public Function PrintOrPreview(ReportName As String, Optional strFilter As String, _
Optional strFile As String, Optional AlternateReportName As String, Optional Warning As String) As Boolean
' returns 1 when completed ReportName is name of report to print, strFilter is filter to use to print report
' give user option of canceling [2], printing immediately [7] or previewing on screen first [1]
' if the value DoNotAsk is passed to strFile, then user will NOT be asked if report should be sent to file
' if user selects file option, a file location dialog box automatically comes up as part of the output statement _
and strFile is shown as the default file name
' AlternateFileName is the report to use when sending to a file, since filtering is not available
' Warning is the message to show to the user when sending to a file (example: All data will be reported)
On Error GoTo Err_PrintOrPreview
Dim intPrintHow As Integer
Dim Response As Integer
Dim strPath As String
Dim strFileName As String
intPrintHow = MsgBox("Preview report on screen?", vbYesNoCancel, "Print or Preview Report"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
' allow sending report to a file only when the value "DoNotAsk" has NOT has been passed to this function
If strFile <> "DoNotAsk" Then
intPrintHow = MsgBox("Send to file?", 259, "Send to file or print"
' Yes/No/Cancel with No as default
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
Else
If intPrintHow <> 6 Then
Exit Function
Else
If intPrintHow = 6 Then
' note report is NOT filtered when sent to file
If Not IsNull(Warning) Then
Response = MsgBox(Warning, vbOKCancel, "Warning when sending report to a file"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If Response <> 1 Then ' 1 returned when user clicks OK
MsgBox "Report cancelled!", vbExclamation
Exit Function
End If
End If
' ask user for path to save file and then ask for file name (showing default name)
strFileName = InputBox("Enter name of file (must end in .RTF)", "Name of file", strFile)
If IsNull(strFileName) Then
MsgBox "Report cancelled -- no file name specified!", vbExclamation
Exit Function
End If
If Len(strFileName) < 4 Or MID$(strFileName, Len(strFileName) - 3, 4) <> ".RTF" Then
strFileName = strFileName & ".RTF"
End If
strPath = InputBox("Where should the file be saved (enter the path)?", "Location to save file"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
If IsNull(strPath) Or strPath < " 0" Then
MsgBox "Report cancelled -- no path specified!", vbExclamation
Exit Function
End If
If MID$(strPath, Len(strPath), 1) <> "\" Then
strPath = strPath & "\"
End If
strFile = strPath & strFileName
If IsNull(AlternateReportName) Then
DoCmd.OutputTo acOutputReport, ReportName, acFormatRTF, strFile
Else
DoCmd.OutputTo acOutputReport, AlternateReportName, acFormatRTF, strFile
End If
Else
MsgBox "Report cancelled!", vbExclamation
End If
End If
End If
Else
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
End If
Else
DoCmd.OpenReport ReportName, acViewPreview, , strFilter
End If
Exit_PrintOrPreview:
Exit Function
Err_PrintOrPreview:
Select Case Err
Case 2501 ' message about invoking Cancel action (to be ignored)
Resume Exit_PrintOrPreview
End Select
MsgBox Err.Description
Resume Exit_PrintOrPreview
End Function
Public Function PrintOrPreview(ReportName As String, Optional strFilter As String, _
Optional strFile As String, Optional AlternateReportName As String, Optional Warning As String) As Boolean
' returns 1 when completed ReportName is name of report to print, strFilter is filter to use to print report
' give user option of canceling [2], printing immediately [7] or previewing on screen first [1]
' if the value DoNotAsk is passed to strFile, then user will NOT be asked if report should be sent to file
' if user selects file option, a file location dialog box automatically comes up as part of the output statement _
and strFile is shown as the default file name
' AlternateFileName is the report to use when sending to a file, since filtering is not available
' Warning is the message to show to the user when sending to a file (example: All data will be reported)
On Error GoTo Err_PrintOrPreview
Dim intPrintHow As Integer
Dim Response As Integer
Dim strPath As String
Dim strFileName As String
intPrintHow = MsgBox("Preview report on screen?", vbYesNoCancel, "Print or Preview Report"
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
' allow sending report to a file only when the value "DoNotAsk" has NOT has been passed to this function
If strFile <> "DoNotAsk" Then
intPrintHow = MsgBox("Send to file?", 259, "Send to file or print"
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
Else
If intPrintHow <> 6 Then
Exit Function
Else
If intPrintHow = 6 Then
' note report is NOT filtered when sent to file
If Not IsNull(Warning) Then
Response = MsgBox(Warning, vbOKCancel, "Warning when sending report to a file"
If Response <> 1 Then ' 1 returned when user clicks OK
MsgBox "Report cancelled!", vbExclamation
Exit Function
End If
End If
' ask user for path to save file and then ask for file name (showing default name)
strFileName = InputBox("Enter name of file (must end in .RTF)", "Name of file", strFile)
If IsNull(strFileName) Then
MsgBox "Report cancelled -- no file name specified!", vbExclamation
Exit Function
End If
If Len(strFileName) < 4 Or MID$(strFileName, Len(strFileName) - 3, 4) <> ".RTF" Then
strFileName = strFileName & ".RTF"
End If
strPath = InputBox("Where should the file be saved (enter the path)?", "Location to save file"
If IsNull(strPath) Or strPath < " 0" Then
MsgBox "Report cancelled -- no path specified!", vbExclamation
Exit Function
End If
If MID$(strPath, Len(strPath), 1) <> "\" Then
strPath = strPath & "\"
End If
strFile = strPath & strFileName
If IsNull(AlternateReportName) Then
DoCmd.OutputTo acOutputReport, ReportName, acFormatRTF, strFile
Else
DoCmd.OutputTo acOutputReport, AlternateReportName, acFormatRTF, strFile
End If
Else
MsgBox "Report cancelled!", vbExclamation
End If
End If
End If
Else
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
End If
Else
DoCmd.OpenReport ReportName, acViewPreview, , strFilter
End If
Exit_PrintOrPreview:
Exit Function
Err_PrintOrPreview:
Select Case Err
Case 2501 ' message about invoking Cancel action (to be ignored)
Resume Exit_PrintOrPreview
End Select
MsgBox Err.Description
Resume Exit_PrintOrPreview
End Function