|
-
Mar 26th, 2003, 01:37 PM
#1
Thread Starter
New Member
Excel Object not getting released
Hi all,
I have crated a VB app that copies data from Access Database to an existing Excel Sheet. It does every fine except that
when I destroy the Excel Object "set Oxl = nothing", it never releases it from the memory. I could see the Excel Process running in the background even after the procedure is finished executing. I have looked several places and can't find anything.
Any help is appreciated.
Thanks
public sub ImportData()
Dim oXl As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXl = CreateObject("Excel.Application")
Set oWB = oXl.Workbooks.Open(txtPath.Text)
For Each oSheet In Worksheets
oSheet.Activate
oSheet.Range("B17").Value = "Blah"
Loop
oWB.Save
oWB.Saved = True
oXl.Workbooks.Close
oXl.Quit
Set oWB = Nothing
Set oXl = Nothing
end sub
-
Mar 26th, 2003, 01:43 PM
#2
Frenzied Member
Looks OK, however if your procedure was interrupted (never reached the end) for some reason only you may know (you were debugging, stepping through the code, etc) then object might never got released so it will hang in there until you shut down Windows. Perhaps, there is some other reasons.
-
Mar 26th, 2003, 01:48 PM
#3
Frenzied Member
Same thing happens on my machine with your code. But the EXCEL process does stop once the VB App closes.
-
Mar 26th, 2003, 01:48 PM
#4
Sleep mode
Originally posted by McGenius
Looks OK, however if your procedure was interrupted (never reached the end) for some reason only you may know (you were debugging, stepping through the code, etc) then object might never got released so it will hang in there until you shut down Windows. Perhaps, there is some other reasons.
huh !
Try this
oXl.Workbooks.Close
-
Mar 26th, 2003, 01:49 PM
#5
Frenzied Member
-
Mar 26th, 2003, 01:52 PM
#6
Sleep mode
lol I can't read unformated code
-
Mar 26th, 2003, 01:53 PM
#7
Frenzied Member
Actually, you may have been onto something, though. The problem was that oXl.Workbooks.Close is different than closing the Workbook object. So you need to do oWB.Close (after you save it) and then you can do oXl.Workbooks.Close
I put this code in (new stuff is in bold), and the Excel process stops:
VB Code:
Private Sub Command1_Click()
Dim oXl As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXl = CreateObject("Excel.Application")
Set oWB = oXl.Workbooks.Open("C:\Public\ExcelProject1\ExcelProject1.xls")
For Each oSheet In oXl.Worksheets
oSheet.Activate
oSheet.Range("B17").Value = "Blah"
Next
oWB.Save
oWB.Saved = True ' I dont think this is necessary, but I understand your intent
[b]oWB.Close[/b]
oXl.Workbooks.Close
oXl.Quit
Set oWB = Nothing
Set oXl = Nothing
End Sub
Last edited by seaweed; Mar 26th, 2003 at 02:09 PM.
~seaweed
-
Mar 26th, 2003, 02:06 PM
#8
Frenzied Member
I just updated the code above. It's a simple one-liner addition!
-
Mar 26th, 2003, 02:11 PM
#9
Sleep mode
So the trick was in closing "oWB" obj ? That was my idea though
-
Mar 26th, 2003, 02:35 PM
#10
Frenzied Member
hehe...that was almost your idea...you suggested oXl.Workbooks.Close, which he was already doing!
-
Mar 26th, 2003, 02:42 PM
#11
Thread Starter
New Member
I have tried oWB.Close. That still doen't work.
Plus I am making sure that I finish the subroutine. So the
the line Set oXL = Nothing gets executed.
Public Sub ImportData()
Dim oXl As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXl = CreateObject("Excel.Application")
Set oWB = oXl.Workbooks.Open(txtPath.Text)
For Each oSheet In Worksheets
oSheet.Activate
oSheet.Range("B17").Value = "Blah"
Next
oWB.Save
oWB.Saved = True
oWB.Close
oXl.Workbooks.Close
oXl.Quit
Set oWB = Nothing
Set oXl = Nothing
End Sub
-
Mar 26th, 2003, 02:44 PM
#12
-
Mar 26th, 2003, 02:51 PM
#13
Frenzied Member
Try changing this:
VB Code:
For Each oSheet In Worksheets
to this:
VB Code:
For Each oSheet In oXl.Worksheets
That's the only difference I see.
-
Mar 26th, 2003, 03:01 PM
#14
Thread Starter
New Member
That fix the problem. Thanks a lot guys.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|