473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Print Excel file from command button

53
Hello,

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

Thanks
Lance
Jan 29 '08 #1
13 14805
nico5038
3,080 Expert 2GB
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
araman
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
254 Expert 100+
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
8,834 Expert 8TB
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
araman
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
araman
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
8,834 Expert 8TB
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
araman
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
araman
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
254 Expert 100+
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
araman
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
254 Expert 100+
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
araman
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

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

Similar topics

0
by: HuggedRugger | last post by:
Hi, Can anyone help me with this. I have a datagrid on a web page I want to send to an excel sheet to print as a report. I send the data using command from my aspx.vb file which works...
4
by: Martin | last post by:
There is an Access table on the network. 15 users who do not have Access are connected to the network. Is there a way for each user to be able to enter one or more rows containing 3 or 4 columns to...
1
by: Brian Turner | last post by:
I have a filenane defined in a table in my database. This filename field is also displayed on my form. I have place a print button on my form. I would like to be abel to click the Print button...
2
by: Steve Chatham | last post by:
I use the following code: Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged Dim sFile As String =...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
2
by: iloveprincess | last post by:
Hi, I'm developing windows application using VB.Net 2005. I would like to send 'save' message using 'SendMessage' API to the excel appication. I've already got a handle of the excel window with...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
3
by: JFKJr | last post by:
Hello everyone, the following is the code which opens an excel file and creates a toolbar button. And, when I click the toolbar button as shown at line #28, the code should call the "Example"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.