Closing Excel


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Closing Excel

  1. #1
    Miroslav St. Jeliaskoff Guest

    Closing Excel

    Hi,
    I have an application that opens and interacts with a workbook. Everything is OK, but when I close the excel application:

    xlApplication = New Excel.Application()
    xlWorkbook = xlApplication.Workbooks.Open(strTemplate)
    xlSheet = xlWorkbook.Worksheets("Calculations Sheet")
    '... some code here

    xlWorkbook.Save()
    xlWorkbook.Close(False)

    xlApplication.Quit() 'here I see the excel closing
    xlApplication = Nothing

    I see how the Excel on the screen is closing because it's visible, but in the Task Manager of my WinXP there is an Excel.exe standing and taking 9MB of memory. And if I close my application the excel.exe goes away. But I want to have it closed when I want - xlApplication.Quit()

    I don't want this - of course, but what must I do? (Windows XP, Excel XP, VB.NET)

    Thanks guys,
    Miroslav St. Jeliaskoff


  2. #2
    Phil Weber Guest

    Re: Closing Excel

    > I see how the Excel on the screen is closing because it's
    > visible, but in the Task Manager of my WinXP there is
    > an Excel.exe standing and taking 9MB of memory.


    Miroslav:

    http://support.microsoft.com/default...EN-US;q317109&

    --
    Phil Weber



  3. #3
    Miroslav St. Jeliaskoff Guest

    Re: Closing Excel

    Strangely it does not work!
    I used the MS suggestion in Q317109 but still Excel "hold on".
    Here is the code:

    'Excel objects
    Private xlApplication As Excel.Application
    Private xlWorkbook As Excel.Workbook
    Private xlSheet As Excel.Worksheet

    Friend Function PrintToExcel() As Integer
    'Open the template file
    xlApplication = New Excel.Application()
    xlWorkbook = xlApplication.Workbooks.Open(Template)
    xlSheet = xlWorkbook.Worksheets("Calculations Sheet")
    'Save all in cells
    With xlSheet
    'Save "general test data"
    .Range("B2").Value = objFinalResults.TestID
    .Range("B3").Value = objTestSystem.TestDate
    .Range("B4").Value = objFinalResults.Request
    End With

    'Save file
    xlWorkbook.Save()

    'Save in a new file
    xlWorkbook.SaveCopyAs("RPT-" & objFinalResults.TestID & ".xls")

    'Print
    xlSheet = xlWorkbook.Worksheets("Brief Report")
    xlSheet.PrintOut()

    'Close Excel
    Call NAR(xlSheet)
    xlWorkbook.Close(False)
    Call NAR(xlWorkbook)
    xlApplication.Quit()
    Call NAR(xlApplication)

    GC.Collect() 'Garbage Collection

    Return 0 'OK

    End Function

    The NAR sub is the same as on Microsoft's site.
    Am I'm missing something?

    Thanks
    Miroslav St. Jeliaskoff

    =============

    "Phil Weber" <pweber@nospam.fawcette.com> wrote in message news:3d907be6$1@10.1.10.29...
    > > I see how the Excel on the screen is closing because it's
    > > visible, but in the Task Manager of my WinXP there is
    > > an Excel.exe standing and taking 9MB of memory.

    >
    > Miroslav:
    >
    > http://support.microsoft.com/default...EN-US;q317109&
    >
    > --
    > Phil Weber
    >
    >



  4. #4
    John Standen Guest

    Re: Closing Excel


    "Miroslav St. Jeliaskoff" <miroslav@earthling.net> wrote:
    >
    >
    >Strangely it does not work!
    >I used the MS suggestion in Q317109 but still Excel "hold on".
    >Here is the code:
    >
    >'Excel objects
    >Private xlApplication As Excel.Application
    >Private xlWorkbook As Excel.Workbook
    >Private xlSheet As Excel.Worksheet
    >
    >Friend Function PrintToExcel() As Integer
    > 'Open the template file
    > xlApplication =3D New Excel.Application()
    > xlWorkbook =3D xlApplication.Workbooks.Open(Template)
    > xlSheet =3D xlWorkbook.Worksheets("Calculations Sheet")
    > 'Save all in cells
    > With xlSheet
    > 'Save "general test data"
    > .Range("B2").Value =3D objFinalResults.TestID
    > .Range("B3").Value =3D objTestSystem.TestDate
    > .Range("B4").Value =3D objFinalResults.Request
    > End With
    >
    > 'Save file
    > xlWorkbook.Save()
    >
    > 'Save in a new file
    > xlWorkbook.SaveCopyAs("RPT-" & objFinalResults.TestID & ".xls")
    > =20
    > 'Print
    > xlSheet =3D xlWorkbook.Worksheets("Brief Report")
    > xlSheet.PrintOut()
    > =20
    > 'Close Excel
    > Call NAR(xlSheet)
    > xlWorkbook.Close(False)
    > Call NAR(xlWorkbook)
    > xlApplication.Quit()
    > Call NAR(xlApplication)
    >
    > GC.Collect() 'Garbage Collection
    >
    > Return 0 'OK
    >
    >End Function
    >
    >The NAR sub is the same as on Microsoft's site.
    >Am I'm missing something?
    >
    >Thanks
    >Miroslav St. Jeliaskoff
    >
    >=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    >
    >"Phil Weber" <pweber@nospam.fawcette.com> wrote in message =
    >news:3d907be6$1@10.1.10.29...
    >> > I see how the Excel on the screen is closing because it's=20
    >> > visible, but in the Task Manager of my WinXP there is=20
    >> > an Excel.exe standing and taking 9MB of memory.=20

    >>=20
    >> Miroslav:=20
    >>=20
    >> http://support.microsoft.com/default...EN-US;q317109&
    >>=20
    >> --=20
    >> Phil Weber
    >>=20
    >>=20

    >
    >
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    ><HTML><HEAD>
    ><META http-equiv=3DContent-Type content=3D"text/html; =
    >charset=3Diso-8859-1">
    ><META content=3D"MSHTML 6.00.2719.2200" name=3DGENERATOR>
    ><STYLE></STYLE>
    ></HEAD>
    ><BODY bgColor=3D#ffffff background=3D"">
    ><DIV><FONT face=3D"Courier New" size=3D2>Strangely it does not =
    >work!<BR>I used the=20
    >MS suggestion in Q317109 but still Excel "hold on".<BR>Here is the=20
    >code:<BR><FONT color=3D#ffffff size=3D2></DIV>
    ><DIV></FONT><FONT size=3D2>'Excel objects</DIV></FONT><FONT size=3D2>
    ><DIV></FONT><FONT size=3D2>Private</FONT><FONT size=3D2> xlApplication =
    ></FONT><FONT=20
    >size=3D2>As</FONT><FONT size=3D2> Excel.Application</DIV>
    ><DIV></FONT><FONT size=3D2>Private</FONT><FONT size=3D2> xlWorkbook =
    ></FONT><FONT=20
    >size=3D2>As</FONT><FONT size=3D2> Excel.Workbook</DIV>
    ><DIV></FONT><FONT size=3D2>Private</FONT><FONT size=3D2> xlSheet =
    ></FONT><FONT=20
    >size=3D2>As</FONT><FONT size=3D2> Excel.Worksheet</DIV>
    ><DIV></FONT><BR><FONT size=3D2>Friend</FONT><FONT size=3D2> </FONT><FONT

    =
    >
    >size=3D2>Function</FONT><FONT size=3D2> PrintToExcel(</FONT><FONT =
    >size=3D2>)=20
    ></FONT><FONT size=3D2>As</FONT><FONT size=3D2> </FONT><FONT =
    >size=3D2>Integer</DIV>
    ><DIV></FONT>*** 'Open the template =
    >file<BR>***=20
    >xlApplication =3D New Excel.Application()<BR>*** =
    >xlWorkbook =3D=20
    >xlApplication.Workbooks.Open(Template)<BR>*** xlSheet =3D =
    >
    >xlWorkbook.Worksheets("Calculations Sheet")<BR>*** 'Save =
    >all in=20
    >cells<BR>*** With=20
    >xlSheet<BR>******* 'Save "general =
    >test=20
    >data"<BR>******* .Range("B2").Value =
    >=3D=20
    >objFinalResults.TestID<BR>*******=20
    >..Range("B3").Value =3D=20
    >objTestSystem.TestDate<BR>*******=20
    >..Range("B4").Value =3D objFinalResults.Request<BR>*** End =
    >
    >With<BR></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>*** 'Save=20
    >file<BR>*** xlWorkbook.Save()<BR></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>*** 'Save in a =
    >new=20
    >file</FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>***=20
    >xlWorkbook.SaveCopyAs("RPT-" & objFinalResults.TestID &=20
    >".xls")<BR>*** </FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>***=20
    >'Print<BR>*** xlSheet =3D xlWorkbook.Worksheets("Brief=20
    >Report")<BR>*** xlSheet.PrintOut()<BR>***=20
    ></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>*** 'Close=20
    >Excel<BR>*** Call NAR(xlSheet)<BR>***=20
    >xlWorkbook.Close(False)<BR>*** Call=20
    >NAR(xlWorkbook)<BR>*** =
    >xlApplication.Quit()<BR>***=20
    >Call NAR(xlApplication)<BR></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>*** GC.Collect() =
    >'Garbage=20
    >Collection<BR></DIV></FONT>
    ><DIV><FONT face=3D"Courier New" size=3D2>*** Return 0=20
    >'OK<BR></DIV></FONT>
    ><DIV><FONT face=3D"Courier New" size=3D2>End Function<BR></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>The NAR sub is the same as on =
    >Microsoft's=20
    >site.</FONT><FONT face=3D"Courier New" size=3D2></FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>Am I'm missing =
    >something?</FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2></FONT>*</DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>Thanks</FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2>Miroslav St. =
    >Jeliaskoff</FONT></DIV>
    ><DIV><FONT face=3D"Courier New" size=3D2></FONT>*</DIV>
    ><DIV><FONT face=3D"Courier New" =
    >size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</DIV></FONT>
    ><DIV><FONT face=3D"Courier New" size=3D2><BR>"Phil Weber"=20
    ><pweber@nospam.fawcette.com> wrote in message=20
    >news:3d907be6$1@10.1.10.29...<BR>> > I see how the Excel on the =
    >screen is=20
    >closing because it's <BR>>* > visible, but in the Task =
    >Manager of my=20
    >WinXP there is <BR>>* > an Excel.exe standing and taking 9MB =
    >of=20
    >memory. <BR>> <BR>> Miroslav: <BR>> =
    ><BR>>****=20
    >http://support.microsoft.com/default...EN-US;q317109&<BR=
    >>>=20

    ><BR>> -- <BR>> Phil Weber<BR>> <BR>> =
    ></FONT></DIV></BODY></HTML>
    >
    >

    Miroslav,

    Look at the troubleshooting at the bottom of the KB article Phil pointed
    you to. I had this problem with an App I developed and found that as long
    as I called System.Runtime.InteropServices.Marshal.ReleaseComObject and GC.Collect
    the reference being held by the .Net Wrapper would be released and all was
    well.

  5. #5
    Miroslav St. Jeliaskoff Guest

    Re: Closing Excel


    "John Standen" <john.standen@originenergy.com.au> wrote in
    message news:3d9ba9d8$1@10.1.10.29...
    >
    > "Miroslav St. Jeliaskoff" <miroslav@earthling.net> wrote:
    > >
    > >
    > >Strangely it does not work!
    > >I used the MS suggestion in Q317109 but still Excel "hold

    on".
    > >Here is the code:
    > >
    > >'Excel objects
    > >Private xlApplication As Excel.Application
    > >Private xlWorkbook As Excel.Workbook
    > >Private xlSheet As Excel.Worksheet
    > >
    > >Friend Function PrintToExcel() As Integer
    > > 'Open the template file
    > > xlApplication =3D New Excel.Application()
    > > xlWorkbook =3D xlApplication.Workbooks.Open(Template)
    > > xlSheet =3D xlWorkbook.Worksheets("Calculations Sheet")
    > > 'Save all in cells
    > > With xlSheet
    > > 'Save "general test data"
    > > .Range("B2").Value =3D objFinalResults.TestID
    > > .Range("B3").Value =3D objTestSystem.TestDate
    > > .Range("B4").Value =3D objFinalResults.Request
    > > End With
    > >
    > > 'Save file
    > > xlWorkbook.Save()
    > >
    > > 'Save in a new file
    > > xlWorkbook.SaveCopyAs("RPT-" & objFinalResults.TestID &

    ".xls")
    > >
    > > 'Print
    > > xlSheet =3D xlWorkbook.Worksheets("Brief Report")
    > > xlSheet.PrintOut()
    > >
    > > 'Close Excel
    > > Call NAR(xlSheet)
    > > xlWorkbook.Close(False)
    > > Call NAR(xlWorkbook)
    > > xlApplication.Quit()
    > > Call NAR(xlApplication)
    > >
    > > GC.Collect() 'Garbage Collection
    > >
    > > Return 0 'OK
    > >
    > >End Function
    > >
    > >The NAR sub is the same as on Microsoft's site.
    > >Am I'm missing something?
    > >
    > >Thanks
    > >Miroslav St. Jeliaskoff
    > >

    >
    > Look at the troubleshooting at the bottom of the KB article

    Phil pointed
    > you to. I had this problem with an App I developed and found

    that as long
    > as I called

    System.Runtime.InteropServices.Marshal.ReleaseComObject and
    GC.Collect
    > the reference being held by the .Net Wrapper would be

    released and all was
    > well.


    The NAR() sub I'm using is:

    Private Sub NAR(ByVal obj As Object)
    Try

    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    Catch
    Finally
    obj = Nothing
    End Try
    End Sub

    so this code is for closing the Excel:

    Call NAR(xlSheet)
    xlWorkbook.Close(False)
    Call NAR(xlWorkbook)
    xlApplication.Quit()
    Call NAR(xlApplication)

    GC.Collect() 'Garbage Collection

    BUT I'll do it the hard way. I'll open an Excel.Application
    when I start the program and will try to close it only at it's
    end.

    Sorry!
    Miro






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center