|
-
Aug 17th, 2001, 12:31 AM
#1
Thread Starter
Lively Member
Cannot Close Excel
My code is
Code:
Public xApp As Excel.Application
Private Sub Command1_Click()
Dim wb As New Excel.Workbook, str As String
Set xApp = CreateObject("Excel.Application")
xApp.Visible = True
str = "Myfile.xls"
Set wb = xApp.Workbooks.Open(FileName:=str)
wb.Close False
End Sub
Private Sub Command2_Click()
xApp.Quit
Set xApp = Nothing
End Sub
When I click on Command2, Excel window is closed but an Excel process always appears in Window Task Manager,Process Tab. If I run above code again, two Excel processes are in Window Task Manager and so on.
Anyone can help me to terminate Excel processes?
-
Aug 17th, 2001, 12:56 AM
#2
Try setting wb to nothing also:
VB Code:
Public xApp As Excel.Application
Private Sub Command1_Click()
Dim wb As New Excel.Workbook, str As String
Set xApp = CreateObject("Excel.Application")
xApp.Visible = True
str = "Myfile.xls"
Set wb = xApp.Workbooks.Open(FileName:=str)
wb.Close False
'set this to nothing also
set wb=nothing
End Sub
Private Sub Command2_Click()
xApp.Quit
Set xApp = Nothing
End Sub
-
Aug 17th, 2001, 01:02 AM
#3
Hyperactive Member
The most common problem many users faced....
Use
Dim xls as excelobject
then
Set xls = new excelobject
If you use
Dim xls as NEW excelobject, you are telling VB to get xls ready for a new instance whenever you call it again after you destroy the first instance.
Therefore if you use this :
Dim myobject as NEW object
myobject.Color = "Red"
set myobject = nothing
myobject.Color = "Blue"
Myobject will be automatically created as a new object without you having to use the set statement...
Therefore, your problem is :
Instead of
Dim wb As New Excel.Workbook, str As String
Use this
Dim wb as Excel.Workbook
William T
Software Architect / Chief Software Developer
Softwaremaker.Net Pte Ltd
http://www.Softwaremaker.net
*** Things are always the darkest before they go pitch black ***
-
Aug 17th, 2001, 03:56 AM
#4
Thread Starter
Lively Member
Hey, I found the problem. The Excel file is too large. IF I open a medium Excel file, error doesnot occur
-
Aug 17th, 2001, 08:55 AM
#5
Fanatic Member
Never use Dim As New.....causes too many problems.
VB Code:
Dim MyXL As Excel.Application
On Error Resume Next
' see if it's open already
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
' if not...open it
Set MyXL = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
MyXL.Quit
Set MyXL = Nothing
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
|