Click to See Complete Forum and Search --> : closing and printing an excel worksheet without saving changes


shaminda
April 25th, 2001, 07:34 AM
There is a program in Visual Basic that opens an exciting excel 97 worksheet and writes to it. I want my program to write to the excel worksheet print it and close the object without saving the changes. Write now the program opens the excel 97 worksheet, and I have to manualy close the program. And it asks whether to save the changes. So buy just clicking a button in VB, the program should
1) write to the excel worksheet
2) print the worksheet to printer
3) close the worksheet without saving changes.

How do I do this?

Thank You!

Kdev
April 25th, 2001, 09:25 AM
This code works for Excel 2000 not sure about 97.

sub Command1_Click()
dim xlApp as Excel.Application
dim xlBook as Workbook
dim xlSheet as Worksheet


set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.Workbooks.Open("myBook.xls")
set xlSheet = xlBook.Worksheets(1)


xlSheet.Cells(1, 1) = "Something" 'Write to cell
xlSheet.PrintOut 'print worksheet
xlApp.DisplayAlerts = false 'Do not prompt to save changes
xlBook.Close 'Will close just the workbook
xlApp.Quit 'Will close the entire excel application

set xlSheet = nothing
set xlBook = nothing
set xlApp = nothing
end sub



-K