Results 1 to 5 of 5

Thread: Cannot Close Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Viet Nam
    Posts
    98

    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?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try setting wb to nothing also:
    VB Code:
    1. Public xApp As Excel.Application
    2. Private Sub Command1_Click()
    3.     Dim wb As New Excel.Workbook, str As String
    4.     Set xApp = CreateObject("Excel.Application")    
    5.     xApp.Visible = True
    6.     str = "Myfile.xls"
    7.     Set wb = xApp.Workbooks.Open(FileName:=str)
    8.    
    9.     wb.Close False
    10. 'set this to nothing also
    11.     set wb=nothing
    12. End Sub
    13.  
    14. Private Sub Command2_Click()
    15.     xApp.Quit
    16.     Set xApp = Nothing
    17. End Sub

  3. #3
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322
    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 ***

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Viet Nam
    Posts
    98
    Hey, I found the problem. The Excel file is too large. IF I open a medium Excel file, error doesnot occur

  5. #5
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Never use Dim As New.....causes too many problems.

    VB Code:
    1. Dim MyXL As Excel.Application
    2.    
    3.     On Error Resume Next
    4.  
    5.     ' see if it's open already
    6.     Set MyXL = GetObject(, "Excel.Application")
    7.  
    8.     If Err.Number <> 0 Then
    9.         ' if not...open it
    10.         Set MyXL = CreateObject("Excel.Application")
    11.     End If
    12.  
    13.     Err.Clear
    14.  
    15.     On Error GoTo 0
    16.  
    17.     MyXL.Quit
    18.     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
  •  



Click Here to Expand Forum to Full Width