Results 1 to 3 of 3
  1. #1
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92

    Need help creating PDF of an Excel sheet with Access VBA

    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.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I don't think OutputTo will work.

    I have code behind Excel file to export to PDF. Perhaps adaptable to Access VBA

    Code:
                ActiveSheet.ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        FileName:=FName, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=True
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    Unfortunately, it returns a compile error: variable not defined, and highlights 'xlTypePDF'. Any idea how I might change it to make it work with Access?

    UPDATE: Never mind. I realized that I didn't have the Excel Object Library turned on. Now that I've turned it on, your code worked! Thanks!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2017, 01:38 AM
  2. Replies: 1
    Last Post: 04-01-2014, 04:18 AM
  3. Help creating Project Status sheet in Access
    By jet_speed in forum Access
    Replies: 7
    Last Post: 09-27-2013, 09:41 PM
  4. Replies: 6
    Last Post: 10-17-2011, 11:16 PM
  5. Replies: 1
    Last Post: 03-02-2011, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums