If the report name needs to be specified as well as the complaint:
As Paul mentioned, the Me. notation won't work for this in a code module.
Code:
Public Sub ExportReport(rptname as string, complaint as variant)
On Error GoTo ExportReport_Err
Dim ReportName As String
Dim criteria As String
Dim fd As Object
Dim FileName As String
Dim k As String
ReportName = rptname
criteria = "[ComplaintNumber]= " & complaint
Set fd = Application.FileDialog(2)
FileName = "Exported Report" & Complaint & ".pdf"
With fd
.Title = "Save to PDF"
.InitialFileName = "\Documents\" & FileName
If .Show = -1 Then
FileName = fd.SelectedItems(1)
If InStr(FileName, ".") = 0 Then
FileName = FileName & ".pdf"
ElseIf Right(FileName, 4) <> ".pdf" Then
k = InStrRev(FileName, ".") - 1
FileName = Left(FileName, k)
FileName = FileName & ".pdf"
End If
DoCmd.OpenReport ReportName, acViewPreview, , criteria, acHidden
DoCmd.OutputTo acOutputReport, ReportName, acFormatPDF, FileName
DoCmd.Close acReport, ReportName, acSaveNo
End If
End With
Set fd = Nothing
ExportReport_Exit:
Exit Sub
ExportReport_Err:
MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
Resume ExportReport_Exit
End Sub