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


Then the user clicks a button control and the spreadsheet is printed to the default printer.
Who is Participating?
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.

     'Sets the object variable to contain a Microsoft Excel Object.
      Dim xlObj As Object
      'Clears the xlObj object.
      Set xlObj = Nothing
      'Sets the xlObj object as a Microsoft Excel Application object.
      Set xlObj = CreateObject("Excel.Application")
      'Opens the file called Book1.xls.
      xlObj.Workbooks.Open filename:=<yourfilename>
      'The variable strVar holds the contents of cell A1 on Sheet1 in
      'the Book1.xls file.
      'Clears the xlObj object.
      Set xlObj = Nothing


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
Bob LearnedCommented:
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.
rb982996Author Commented:
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.


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


Dim xlWB as Object

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

xlWB.Save 'will save the file

rb982996Author Commented:
Adjusted points to 150
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
Visual Basic Classic

From novice to tech pro — start learning today.