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!

How to close Excel In Visual Basic

Status
Not open for further replies.

andypower

Programmer
Aug 10, 2004
23
0
0
IN
Hi,

Dim myBook As Excel.Workbook
Dim exObj As Excel.Application

Set myBook = Workbooks.Open(filename:=StrPath)

Set exObj = New Excel.Application
exObj.ActiveWorkbook.Worksheets(cnt + 1).Activate

(some code)

mybook.close
exobj.activeworkbook.close

I have use these stmt to open excel file then using 'exobj' object i have activate particular sheet from workbook.
After that, selected sheet's data is inserted in database.
But i have problem with closing these object.
when application(VB Project) is closed it will display msg as
"excelprj.xls is now enable to read-write"
and that excelsheet is opened.I want to close it.

can you tell me how to close it.
 
You need to quit the application and set the object to Nothing

exObj.Quit
Set exObj = Nothing

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
mybook.Close()
mybook = Nothing
exObj.Quit()
exObj = Nothing
 
Let's try that again:

mybook.Close
Set mybook = Nothing
exObj.Quit
Set exObj = Nothing
 
Dim myBook As Excel.Workbook
Dim exObj As object

Set myBook = Workbooks.Open(filename:=StrPath)

Set exObj = createObject("excelApplication")
exObj.ActiveWorkbook.Worksheets(cnt + 1).Activate

(some code)

mybook.close
set mybook=nothing
exobj.usercontrol=false
exobj.quit
set exobj=nothing

I have use these stmt to open excel file then using 'exobj' object i have activate particular sheet from workbook.
After that, selected sheet's data is inserted in database.
But i have problem with closing these object.

even i closed the object then also instance of excel is running
in task manager window

or some times display msg as sheet is read only

this prblm creates me problem
so plz help me
 
I notice that you've asked 17 questions and as yet you haven't acknowledged any answers. If you really haven't found any answers that work for you read faq222-2244 to see how to ask better questions. If you have had satisfactory answers then read faq222-2244 to see how to acknowledge them.

For this question, are you sure all your workbooks are closed first?
Code:
Dim W As Workbook
For Each W In Workbooks
   W.Close savechanges:=False
Next W

Then quit the app and set the app object to nothing

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Does the rest of your code work? First you open a workbook (Workbooks.Open), next create new excel instance (exobj). It is likely that you open the same file in two excel instances. In the second one it will be opened read-only. Assuming that it's OK, you need to quit exobj (exobj.Quit) and the first one (mybook.Parent.Quit), first saving workbooks or cancelling prompt for saving.
Anyway, you could consider:
- when testing, make excel instances visible
- reference workbooks/worksheets directly, by name or index, instead of to active one

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top