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)
     
    ExcelSheet.PrintOut
    ExcelApp.Quit
    Set ExcelApp = Nothing


End Sub

Open in new window

LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
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....

ExcelApp.Dialogs(8).Show

Open in new window


Wayne

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
Thank you very much! Works great!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.