Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Excel Sheet in Visual Basic 1

Status
Not open for further replies.

Kchinnaswamy

IS-IT--Management
Jan 31, 2001
27
0
0
US
Hi

I need some help on how to print an Excel spreadsheet in Visual Basic. The print has to go to the windows default printer.

thanks in advance

Kumar.
 
Kumar,

You need to add the Microsoft Excel 8.0 Object library in your references. Your code then would look like this -

Dim xlsApplication As New Excel.Application
Dim xlsWorkBook As New Excel.Workbook
Dim xlsWorkSheet As New Excel.Worksheet

Set xlsWorkBook = xlsApplication.Workbooks.Add
Set xlsWorkSheet = xlsWorkBook.Worksheets.Add

' Include your code to populate this worksheet

xlsWorkSheet.PageSetup.PrintGridlines = True
xlsWorkSheet.Columns.AutoFit
xlsWorkSheet.PrintOut

' VB starts up Excel to do this printout, so we need to
' close this gracefully. If you want to save a copy of the
' worksheet you created, you might want to set the Saved
' and DisplayAlerts property set accordingly.
xlsWorkBook.Saved = False
xlsWorkBook.Close (False)
xlsApplication.DisplayAlerts = False
xlsApplication.Quit

Set xlsWorkSheet = Nothing
Set xlsWorkBook = Nothing
Set xlsApplication = Nothing

Hope this helps.

Thanks,

- Subha :) Nothing is impossible, even the word impossible says I'm possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top