not quitting excel

Discussion in 'Microsoft Dot NET' started by Mike, Dec 16, 2003.

  1. Mike

    Mike Guest

    Please help. I am using object.quit in an attempt to
    quit an excel application that I started with createobject
    ("Excel.Application"). The code executes, but does not
    stop the application. When I run the exact same code for
    Access and Word, it works fine (app closes).

    Any ideas? Any help would be appreciated.

    Thanks,
    Mike Sweet
     
    Mike, Dec 16, 2003
    #1
    1. Advertisements

  2. have you tried
    System.Runtime.InteropServices.Marshal.ReleaseComObject( excelObj );

    after the quit method
    This ought to do it

    /morten

    "Mike" <> wrote in message
    news:016801c3c406$c58743d0$...
    > Please help. I am using object.quit in an attempt to
    > quit an excel application that I started with createobject
    > ("Excel.Application"). The code executes, but does not
    > stop the application. When I run the exact same code for
    > Access and Word, it works fine (app closes).
    >
    > Any ideas? Any help would be appreciated.
    >
    > Thanks,
    > Mike Sweet
     
    Morten Overgaard, Dec 16, 2003
    #2
    1. Advertisements

  3. Mike

    Guest Guest

    Morten,

    Thank you for such a quick response. I tried your
    suggestion and had no success. What's odd is that this
    approach works if I replaced "excel.application"
    with "word.application" or "access.application". I
    wonder if this has something to do with my installation
    of Excel. But what's also odd is that the quit method
    works for Excel if I run it from an older version of vb.
    It's just not working for vb .net 2003.

    Below is the code. Would appreciate any insights.

    Function validfile(ByVal in_file As String) As Boolean

    Dim excel As Object

    On Error Resume Next
    validfile = True
    excel = GetObject(, "excel.application")
    If Err().Number <> 0 Then
    excel = CreateObject("excel.application")
    b_excelwasnotrunning = True
    Err().Clear() ' Clear Err object in case
    error occurred.
    Else
    b_excelwasnotrunning = False
    End If
    excel.Workbooks.Open(in_file)
    If Err().Number <> 0 Then
    validfile = False
    Err().Clear()
    End If
    If b_excelwasnotrunning = True Then
    excel.quit()

    System.Runtime.InteropServices.Marshal.ReleaseComObject
    (excel)
    excel = Nothing
    End If


    End Function

    Thank you for any additional help.

    Regards,
    Mike Sweet

    >-----Original Message-----
    >have you tried
    >System.Runtime.InteropServices.Marshal.ReleaseComObject(

    excelObj );
    >
    >after the quit method
    >This ought to do it
    >
    >/morten
    >
    >"Mike" <> wrote in

    message
    >news:016801c3c406$c58743d0$...
    >> Please help. I am using object.quit in an attempt to
    >> quit an excel application that I started with

    createobject
    >> ("Excel.Application"). The code executes, but does not
    >> stop the application. When I run the exact same code

    for
    >> Access and Word, it works fine (app closes).
    >>
    >> Any ideas? Any help would be appreciated.
    >>
    >> Thanks,
    >> Mike Sweet

    >
    >
    >.
    >
     
    Guest, Dec 17, 2003
    #3
  4. Mike

    Paul Clement Guest

    On Tue, 16 Dec 2003 16:58:01 -0800, <> wrote:

    ¤ Morten,
    ¤
    ¤ Thank you for such a quick response. I tried your
    ¤ suggestion and had no success. What's odd is that this
    ¤ approach works if I replaced "excel.application"
    ¤ with "word.application" or "access.application". I
    ¤ wonder if this has something to do with my installation
    ¤ of Excel. But what's also odd is that the quit method
    ¤ works for Excel if I run it from an older version of vb.
    ¤ It's just not working for vb .net 2003.
    ¤
    ¤ Below is the code. Would appreciate any insights.
    ¤
    ¤ Function validfile(ByVal in_file As String) As Boolean
    ¤
    ¤ Dim excel As Object
    ¤
    ¤ On Error Resume Next
    ¤ validfile = True
    ¤ excel = GetObject(, "excel.application")
    ¤ If Err().Number <> 0 Then
    ¤ excel = CreateObject("excel.application")
    ¤ b_excelwasnotrunning = True
    ¤ Err().Clear() ' Clear Err object in case
    ¤ error occurred.
    ¤ Else
    ¤ b_excelwasnotrunning = False
    ¤ End If
    ¤ excel.Workbooks.Open(in_file)
    ¤ If Err().Number <> 0 Then
    ¤ validfile = False
    ¤ Err().Clear()
    ¤ End If
    ¤ If b_excelwasnotrunning = True Then
    ¤ excel.quit()
    ¤
    ¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
    ¤ (excel)
    ¤ excel = Nothing
    ¤ End If
    ¤
    ¤
    ¤ End Function
    ¤
    ¤ Thank you for any additional help.
    ¤
    ¤ Regards,
    ¤ Mike Sweet

    Try not to create implicit objects. For example, if you create a Workbook object by opening a
    Workbook, set the returned object to a variable:

    ExcelWorkbook = excel.Workbooks.Open(in_file)

    ExcelWorkbook.Close, False

    ExcelWorkbook = Nothing


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Dec 17, 2003
    #4
  5. Mike

    Mike Guest

    Paul,

    Thank you so much. The following code now does not work
    for me though - it does not close Excel. However, if I
    comment out the 3 excelworkbook lines, it does quit
    Excel. Any thoughts:

    excel = CreateObject("excel.application")
    excelworkbook = excel.Workbooks.Open(in_file)
    excelworkbook.close()
    excelworkbook = Nothing
    excel.quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject
    (excel)
    excel = Nothing

    >-----Original Message-----
    >On Tue, 16 Dec 2003 16:58:01 -0800,

    <> wrote:
    >
    >¤ Morten,

    >¤ Thank you for such a quick response. I tried your
    >¤ suggestion and had no success. What's odd is that

    this
    >¤ approach works if I replaced "excel.application"
    >¤ with "word.application" or "access.application". I
    >¤ wonder if this has something to do with my

    installation
    >¤ of Excel. But what's also odd is that the quit method
    >¤ works for Excel if I run it from an older version of

    vb.
    >¤ It's just not working for vb .net 2003.

    >¤ Below is the code. Would appreciate any insights.

    >¤ Function validfile(ByVal in_file As String) As Boolean

    >¤ Dim excel As Object

    >¤ On Error Resume Next
    >¤ validfile = True
    >¤ excel = GetObject(, "excel.application")
    >¤ If Err().Number <> 0 Then
    >¤ excel = CreateObject("excel.application")
    >¤ b_excelwasnotrunning = True
    >¤ Err().Clear() ' Clear Err object in case
    >¤ error occurred.
    >¤ Else
    >¤ b_excelwasnotrunning = False
    >¤ End If
    >¤ excel.Workbooks.Open(in_file)
    >¤ If Err().Number <> 0 Then
    >¤ validfile = False
    >¤ Err().Clear()
    >¤ End If
    >¤ If b_excelwasnotrunning = True Then
    >¤ excel.quit()

    >¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
    >¤ (excel)
    >¤ excel = Nothing
    >¤ End If


    >¤ End Function

    >¤ Thank you for any additional help.

    >¤ Regards,
    >¤ Mike Sweet
    >
    >Try not to create implicit objects. For example, if you

    create a Workbook object by opening a
    >Workbook, set the returned object to a variable:
    >
    >ExcelWorkbook = excel.Workbooks.Open(in_file)
    >
    >ExcelWorkbook.Close, False
    >
    >ExcelWorkbook = Nothing
    >
    >
    >Paul ~~~
    >Microsoft MVP (Visual Basic)
    >.
    >
     
    Mike, Dec 29, 2003
    #5
  6. Mike

    Paul Clement Guest

    On Mon, 29 Dec 2003 08:00:21 -0800, "Mike" <> wrote:

    ¤ Paul,
    ¤
    ¤ Thank you so much. The following code now does not work
    ¤ for me though - it does not close Excel. However, if I
    ¤ comment out the 3 excelworkbook lines, it does quit
    ¤ Excel. Any thoughts:
    ¤
    ¤ excel = CreateObject("excel.application")
    ¤ excelworkbook = excel.Workbooks.Open(in_file)
    ¤ excelworkbook.close()
    ¤ excelworkbook = Nothing
    ¤ excel.quit()
    ¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
    ¤ (excel)
    ¤ excel = Nothing
    ¤

    Which version of Excel are you using? If you are using XP or 2003 the primary interop assemblies
    might be a better solution.

    http://msdn.microsoft.com/library/d.../html/wrrefofficeprimaryinteropassemblies.asp
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_oxppias.asp

    As a last resort you can use the brute force method (API function calls) to terminate the Excel
    process:

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
    lpWindowName As String) As Int32
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
    As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

    Public Function TerminateExcel()

    Dim ClassName As String
    Dim WindowHandle As Int32
    Dim ReturnVal As Int32
    Const WM_QUIT = &H12

    Do

    ClassName = "XLMain"
    WindowHandle = FindWindow(ClassName, Nothing)

    If WindowHandle Then
    ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
    End If

    Loop Until WindowHandle = 0

    End Function


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Dec 30, 2003
    #6
  7. Mike

    Ron Allen Guest

    Mike,
    You need to ReleaseComObject on your excel workbook and any other excel
    objects that you create before setting them to nothing.
    Ron Allen
    "Mike" <> wrote in message
    news:1e1801c3ce24$dcb2b670$...
    Paul,

    Thank you so much. The following code now does not work
    for me though - it does not close Excel. However, if I
    comment out the 3 excelworkbook lines, it does quit
    Excel. Any thoughts:

    excel = CreateObject("excel.application")
    excelworkbook = excel.Workbooks.Open(in_file)
    excelworkbook.close()
    excelworkbook = Nothing
    excel.quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject
    (excel)
    excel = Nothing

    >-----Original Message-----
    >On Tue, 16 Dec 2003 16:58:01 -0800,

    <> wrote:
    >
    >¤ Morten,

    >¤ Thank you for such a quick response. I tried your
    >¤ suggestion and had no success. What's odd is that

    this
    >¤ approach works if I replaced "excel.application"
    >¤ with "word.application" or "access.application". I
    >¤ wonder if this has something to do with my

    installation
    >¤ of Excel. But what's also odd is that the quit method
    >¤ works for Excel if I run it from an older version of

    vb.
    >¤ It's just not working for vb .net 2003.

    >¤ Below is the code. Would appreciate any insights.

    >¤ Function validfile(ByVal in_file As String) As Boolean

    >¤ Dim excel As Object

    >¤ On Error Resume Next
    >¤ validfile = True
    >¤ excel = GetObject(, "excel.application")
    >¤ If Err().Number <> 0 Then
    >¤ excel = CreateObject("excel.application")
    >¤ b_excelwasnotrunning = True
    >¤ Err().Clear() ' Clear Err object in case
    >¤ error occurred.
    >¤ Else
    >¤ b_excelwasnotrunning = False
    >¤ End If
    >¤ excel.Workbooks.Open(in_file)
    >¤ If Err().Number <> 0 Then
    >¤ validfile = False
    >¤ Err().Clear()
    >¤ End If
    >¤ If b_excelwasnotrunning = True Then
    >¤ excel.quit()

    >¤ System.Runtime.InteropServices.Marshal.ReleaseComObject
    >¤ (excel)
    >¤ excel = Nothing
    >¤ End If


    >¤ End Function

    >¤ Thank you for any additional help.

    >¤ Regards,
    >¤ Mike Sweet
    >
    >Try not to create implicit objects. For example, if you

    create a Workbook object by opening a
    >Workbook, set the returned object to a variable:
    >
    >ExcelWorkbook = excel.Workbooks.Open(in_file)
    >
    >ExcelWorkbook.Close, False
    >
    >ExcelWorkbook = Nothing
    >
    >
    >Paul ~~~
    >Microsoft MVP (Visual Basic)
    >.
    >
     
    Ron Allen, Dec 30, 2003
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dean Bortell

    C# works with Excel 2002 but not Excel 2000?

    Dean Bortell, Nov 17, 2003, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    180
    Robert Jacobson
    Nov 17, 2003
  2. Guest

    DTE.Quit() not quitting.

    Guest, May 7, 2004, in forum: Microsoft Dot NET
    Replies:
    0
    Views:
    335
    Guest
    May 7, 2004
  3. Guest
    Replies:
    1
    Views:
    207
    Mattias Sjögren
    Apr 11, 2005
  4. Darren Barrick via .NET 247

    Excel Automation ? Excel Attaches itself to my Excel Object!

    Darren Barrick via .NET 247, May 20, 2005, in forum: Microsoft Dot NET
    Replies:
    2
    Views:
    251
    Michael D. Ober
    May 20, 2005
  5. John

    dot not app not quitting

    John, Jun 28, 2007, in forum: Microsoft Dot NET
    Replies:
    4
    Views:
    174
    Vadim Chekan
    Jul 2, 2007
Loading...

Share This Page