Link to home
Start Free TrialLog in
Avatar of rb982996
rb982996

asked on

Printing an Excel Spreadsheet

In code, how would I print an excel spreadsheet.  For instance, a user puts an excel name into a text box

c:\tmpDocs\sample.xls

Then the user clicks a button control and the spreadsheet is printed to the default printer.
ASKER CERTIFIED SOLUTION
Avatar of manojamin
manojamin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
What manojamin has included will work, but there is an additional comment that needs to be included.  

I have seen a lot of example code that includes late-binding references (xlObj As Object).  While this is perfectly acceptable, there is a lot of overhead, and slower speeds from late-binding.  Plus you can't use the AutoComplete function of VB, because VB doesn't understand what type the object is until run-time.

The better way would be early-binding:

Add a reference to the Microsoft Office library, and Dim xlObj As New Excel.Application.

You could then ignore the CreateObject line.
Avatar of rb982996
rb982996

ASKER

Hey manojamin, this worked great.  For extra points is there a way to automatically save linked data?  The spreadsheet I am printing is recieving data via DDE and when ever I attempt to print it using the code you gave me I get asked a question if I would like to save the current linked data.

Thanks!
Try

xlObj.SaveWorkSpace("thefilenameyouwant") will save the excel application

or


Dim xlWB as Object

Set xlWB = xlObj.Workbooks.Open (filename:=<yourfilename>)

xlWB.Save 'will save the file



Adjusted points to 150