We help IT Professionals succeed at work.
Get Started

Print Excel File from MS Access

181 Views
Last Modified: 2015-03-22
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

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE