By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,491 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,491 IT Pros & Developers. It's quick & easy.

Print Excel file from command button

P: 53
Hello,

Is it possible to print an excel file from a command button?

Thanks
Lance
Jan 29 '08 #1
Share this Question
Share on Google+
13 Replies


nico5038
Expert 2.5K+
P: 3,072
Check out the Shell command.
This wil allow starting every application, for excel use the excel .exe and the steering parm with the filename to print it.

Nic;o)
Jan 29 '08 #2

P: 53
Check out the Shell command.
This wil allow starting every application, for excel use the excel .exe and the steering parm with the filename to print it.

Nic;o)
thanks, I will see if that works
Jan 29 '08 #3

jaxjagfan
Expert 100+
P: 254
I forget all the specifcs but within Access should be something like this:

Dim xlapp As Excel.Application
xlapp.Workbooks.Open "c:\myworkbook.xls"
xlapp.ActiveSheet.PrintOut
xlapp.Workbooks.Close

This would assume you wanted to print the active sheet (the one in view when you first open excel.
Jan 29 '08 #4

ADezii
Expert 5K+
P: 8,679
Hello,

Is it possible to print an excel file from a command button?

Thanks
Lance
Place the following code in the Click() Event of a Command Button, make your own substitutions, then fire away. The code has been tested and is fully operational. Any questions, please feel free to ask
Expand|Select|Wrap|Line Numbers
  1. '1st ==> Set a Reference to the Microsoft Excel X.X Object Library
  2.  
  3. Dim strPathToExcel As String, strSpreadsheetName As String
  4. Dim strWorksheetName As String
  5.  
  6. Dim ExcelApp As New Excel.Application
  7. Dim ExcelBook As New Excel.Workbook
  8. Dim ExcelSheet As New Excel.Worksheet
  9.  
  10. '******* Substitute your own values ********
  11. strPathToExcel = "C:\Test\"               '*
  12. strSpreadsheetName = "Employees.xls"      '*
  13. strWorksheetName = "Employees"            '*
  14. '*****************************************'*
  15.  
  16. 'Let's not see what is going on
  17. ExcelApp.Visible = False
  18.  
  19. Set ExcelBook = ExcelApp.Workbooks.Open(strPathToExcel & strSpreadsheetName)
  20. Set ExcelSheet = ExcelBook.Worksheets(strWorksheetName)
  21.  
  22. ExcelSheet.PrintOut
  23.  
  24. ExcelApp.Quit
  25. Set ExcelApp = Nothing
Jan 29 '08 #5

P: 53
Place the following code in the Click() Event of a Command Button, make your own substitutions, then fire away. The code has been tested and is fully operational. Any questions, please feel free to ask
Expand|Select|Wrap|Line Numbers
  1. '1st ==> Set a Reference to the Microsoft Excel X.X Object Library
  2.  
  3. Dim strPathToExcel As String, strSpreadsheetName As String
  4. Dim strWorksheetName As String
  5.  
  6. Dim ExcelApp As New Excel.Application
  7. Dim ExcelBook As New Excel.Workbook
  8. Dim ExcelSheet As New Excel.Worksheet
  9.  
  10. '******* Substitute your own values ********
  11. strPathToExcel = "C:\Test\"               '*
  12. strSpreadsheetName = "Employees.xls"      '*
  13. strWorksheetName = "Employees"            '*
  14. '*****************************************'*
  15.  
  16. 'Let's not see what is going on
  17. ExcelApp.Visible = False
  18.  
  19. Set ExcelBook = ExcelApp.Workbooks.Open(strPathToExcel & strSpreadsheetName)
  20. Set ExcelSheet = ExcelBook.Worksheets(strWorksheetName)
  21.  
  22. ExcelSheet.PrintOut
  23.  
  24. ExcelApp.Quit
  25. Set ExcelApp = Nothing


Thank you very much,

that is what i was looking for


Lance
Jan 29 '08 #6

P: 53
Thank you very much,

that is what i was looking for


Lance
i get an error at

Dim ExcelApp As New Excel.Application

That says
compile error
user defined type not defined
Jan 29 '08 #7

ADezii
Expert 5K+
P: 8,679
i get an error at

Dim ExcelApp As New Excel.Application

That says
compile error
user defined type not defined
You probably forgot to set a Reference to the Microsoft Excel X.X Object Library
Jan 30 '08 #8

P: 53
You probably forgot to set a Reference to the Microsoft Excel X.X Object Library
Thanks for the reply.

How would I do that?
Jan 30 '08 #9

P: 53
Thanks for the reply.

How would I do that?
ok figured out how to set the reference.

Now it stops saying the file cannot be found ...the path it show is the location of excel+the path to the file 'c:\excel\c:\documents\file.xls cannot be found'
Jan 30 '08 #10

jaxjagfan
Expert 100+
P: 254
ok figured out how to set the reference.

Now it stops saying the file cannot be found ...the path it show is the location of excel+the path to the file 'c:\excel\c:\documents\file.xls cannot be found'
Post your code

the "c:" cannot exist twice in the path. You don't have to give the path to excel, just the excel spreadsheet
Jan 30 '08 #11

P: 53
Post your code

the "c:" cannot exist twice in the path. You don't have to give the path to excel, just the excel spreadsheet

I removed what's in bold before i got your reply and it worked. any downside to that?

Set ExcelBook = ExcelApp.Workbooks.Open(strPathToExcel & strSpreadsheetName)
Jan 30 '08 #12

jaxjagfan
Expert 100+
P: 254
I removed what's in bold before i got your reply and it worked. any downside to that?

Set ExcelBook = ExcelApp.Workbooks.Open(strPathToExcel & strSpreadsheetName)
What was meant by the code you were given.
strPathToExcel is just the path to the document
strSpreadsheetName is just the name of the document.

If you put it all in one string and removed the other - it's not a problem.
Jan 30 '08 #13

P: 53
What was meant by the code you were given.
strPathToExcel is just the path to the document
strSpreadsheetName is just the name of the document.

If you put it all in one string and removed the other - it's not a problem.

I see, I thought strPathtoExcel was to the excel.exe. That make sense now.

Thanks to all for the help
Jan 30 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.