Search code examples
printingvb6

printing excel sheet from visual basic 6


I need to programmatically send an Excel spreadsheet to our default printer. Is it possible to do this without human interaction?

I can use the PrintFile command to print a text file without an issue, but I cannot get Excel to print this way.

How can I print an Excel document in VB6?


Solution

  • Here is a basic template for printing with Excel:

    Private Sub Command1_Click()
       Dim ExcelApp As Excel.Application
       Set ExcelApp = New Excel.Application
       ExcelApp.Visible = False
       ExcelApp.Workbooks.Open "c:\temp\test.xlsx"
       
       'print the workbook
       Dim ExcelBook As Excel.Workbook
       Set ExcelBook = ExcelApp.ActiveWorkbook
       ExcelBook.PrintOut
       
       'print a worksheet
       Dim ExcelSheet As Excel.Worksheet
       Set ExcelSheet = ExcelApp.Sheets(1)
       ExcelSheet.PrintOut
    
       ExcelApp.Quit
    End Sub
    

    Of course, you will need to set a Reference to Microsoft Excel X.X Object Library.