closing excel in VB

JAN777
03-27-2003, 07:54 AM
I have a problem with closing excel. I can open excel but I can't close it.
I've tried to this

dim Excelwkbk as object
set ExcelWkbk = Workbooks.Close
ExcelApp = ExcelWkbk.Application
ExcelApp.Close (True)

but it doesn't work

can someone tell me how to solve tis problem

Greetzzzzzzzz

Wamphyri
03-27-2003, 08:11 AM
I'll go over opening and closing excel with you from the beginning.

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
'open excel application
Set xlApp = New Excel.Application
'Open excel workbook
Set xlWB = xlApp.Workbooks.Open(Filename:="C:\YourFile.xls")
'do your code here
'Close workbook (optional)
xlWB.Close
'Quit excel (automatically closes all workbooks)
xlApp.Quit
'Clean up memory (you must do this)
Set xlWB = Nothing
Set xlApp = Nothing

JAN777
03-28-2003, 12:28 AM
thanks for your solution. It works, but it is a solution for using 1 button.

It has to open with a form load and close if I press the button 'End'.

I've tried to use your solution in 2 buttons but I can't make it work.

Wamphyri
03-28-2003, 07:55 AM
Same code just move some parts of it to different locations

'Need to declare outside of any Sub
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Private Sub Form_Load()
'open excel application
Set xlApp = New Excel.Application
'Open excel workbook
Set xlWB = xlApp.Workbooks.Open(Filename:="C:\YourFile.xls" )
'do your code here
End Sub

Private Sub cmdClose_Click()
'Close workbook (optional)
xlWB.Close
'Quit excel (automatically closes all workbooks)
xlApp.Quit
'Clean up memory (you must do this)
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

However, instead of Quitting Excel and setting xlWB and xlApp to nothing with a button I'd suggest doing all of that in the Form_Unload Sub.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum