We help IT Professionals succeed at work.

Print Excel File from MS Access

I'm trying to print an excel file from an access command button on my access form. I have the following code that will work but I need some help modifying it to allow for 2 tweaks. One I need to save and close the excel file once the code fires. Currently when the code is finished it asks me to save changes to the excel file or not. I always want it to save changes and close excel. Second instead of printing directly to the default printer I need the printer dialog box to pop up so I can choose which printer I want to send the file to. Can anyone help me with this code?

Sub PrintExcelFile()

    '1st ==> Set a Reference to the Microsoft Excel X.X Object Library
    Dim strPathToExcel As String, strSpreadsheetName As String
    Dim strWorksheetName As String
    Dim ExcelApp As New Excel.Application
    Dim ExcelBook As New Excel.Workbook
    Dim ExcelSheet As New Excel.Worksheet
    '******* Substitute your own values ********
    strPathToExcel = "M:\"
    strSpreadsheetName = "EIA template.xlsx"
    strWorksheetName = "Sheet1"
    'Let's not see what is going on
    ExcelApp.Visible = False
    Set ExcelBook = ExcelApp.Workbooks.Open(strPathToExcel & strSpreadsheetName)
    Set ExcelSheet = ExcelBook.Worksheets(strWorksheetName)
    Set ExcelApp = Nothing

End Sub

Open in new window

Watch Question

To close and save the workbook, use this before quitting Excel...

ExcelBook.Close True

Open in new window

...and to show the print dialog, use this....


Open in new window

Lawrence SalvucciDirector of Information Technology


Thank you very much! Works great!!