[Webinar] Streamline your web hosting managementRegister Today


Printing an Excel Spreadsheet

Posted on 1999-12-06
Medium Priority
Last Modified: 2010-05-02
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.
Question by:rb982996
  • 2
  • 2

Accepted Solution

manojamin earned 600 total points
ID: 2260032
     '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

LVL 96

Expert Comment

by:Bob Learned
ID: 2260089
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.

Author Comment

ID: 2263246
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.


Expert Comment

ID: 2263315

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


Author Comment

ID: 2263337
Adjusted points to 150

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question