Hi everyone. I have created a database for the purpose of outputting some fields onto various Excel sheets and then saving those sheets as PDF files. I'm trying to get the first sheet converted into a PDF, and am not having any luck. I don't think I fully understand how to use the OutputTo method. Here's the code to the sub I've created. I have pieced together this code from a few different websites, and I thought I had it correct, but I keep getting the following error on the line that is in bold:
Runtime error '438'
Object doesn't support this property or method.
Code:
Dim rstQ_DealMemo As Recordset 'global variable
Sub MakePDF(SheetToPrint As String, WorksheetName As String)
Dim strFile As String
Dim FirstName As String
Dim LastName As String
Dim ExcelSheet As Object
Set ExcelSheet = Nothing
FirstName = rstQ_DealMemo.Fields("FirstName")
LastName = rstQ_DealMemo.Fields("LastName")
strFile = Replace(LastName, ".", "_") _
& ", " _
& Replace(FirstName, ".", "_") _
& " - " _
& (SheetToPrint) _
& ".pdf"
strFile = CurrentProject.Path & "\" & strFile
Set ExcelSheet = WB.Worksheets(WorksheetName)
DoCmd.OutputTo ExcelSheet, "", "PDFFormat(*.pdf)", strFile, False, "", 0, acExportQualityPrint
End Sub
Instead of starting the line with DoCmd, I tried ExcelSheet, but that also returned the same error. Obviously there's something I'm not quite understanding. I've searched different forms looking for any clues, but I'm not finding anything. Any help would be appreciated. Thanks!
EDIT: Just to be clear, I have another sub that opens the Excel file and copies the data onto it. So at this point in my code, the Excel file is open, and the sheet I want to print is active.