Re: Excel object cannot released without error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott M.

    Re: Excel object cannot released without error

    Many methods return objects when they are called. With Excel these objects
    are placed in memory and must be destroyed via ReleaseComObjec t as you have
    done with your NAR method, but the line:

    objExcel.Workbo oks.Open(Page.M apPath("reports \BLCost.xls"))

    could be the culprit since the Open method returns a reference to a Workbook
    object that you haven't assigned a variable to. Since you have no variable
    to explicitly use to destroy the reference, the reference counter for that
    object can't get to zero and therefore Excel won't close.

    You should always explicitly assign a variable to all references, that means
    any method or property call that returns an object.

    -Scott

    "Raymond Chiu" <rchiu5hk@yahoo .com.hkwrote in message
    news:edZvvCZ4IH A.1204@TK2MSFTN GP04.phx.gbl...
    Dear All,
    I have the problem which exists for several months and cannot find the
    solution in the web, I see others also have the same situation but they
    are also not solved.
    >
    It is excel object which still remain in the process in the task manager
    in the server. Another project does not have this problem and they are on
    the same server. I have tried to change from win2000 server to win2003
    server, from ms office 2003 to ms office 2007. Excel still remain in the
    process in the task manager. Although it would not affect another excel
    running of another records, I need to clear the excel session everyday in
    the server.
    >
    You can try using the attached excel file to see whether it would cause
    the same problem in your computer. Please Let me know and your ideas.
    Appreciate for your help!!!!
    >
    ---------------------coding -----------------------------------------------------------------------------------
    >
    Dim objExcel As New Excel.Applicati on
    Dim objSheet As Excel.Worksheet
    >
    objExcel.Workbo oks.Open(Page.M apPath("reports \BLCost.xls"))
    >
    objExcel.Displa yAlerts = False
    objSheet = objExcel.Sheets ("Form")
    objSheet.Copy(A fter:=objSheet)
    >
    objSheet = objExcel.Active Sheet
    >
    ............... ............... ............... ..........
    >
    objExcel.Sheets ("Form").Delete ()
    >
    objSheet.SaveAs (Page.MapPath(" ..\out\" & sFileName))
    >
    NAR(objSheet)
    objExcel.Workbo oks.Close()
    NAR(objExcel.Wo rkbooks)
    objExcel.Quit()
    NAR(objExcel)
    GC.Collect()
    >
    ---------------end of
    sub-----------------------------------------------------------------
    >
    Private Sub NAR(ByVal o As Object)
    Try
    System.Runtime. InteropServices .Marshal.Releas eComObject(o)
    >
    Catch ex As Exception
    Dim remarklabel As Label
    remarklabel = CType(FormView1 .FindControl("r emarklabel"),
    Label)
    remarklabel.Tex t = remarklabel.Tex t + ex.ToString()
    Finally
    o = Nothing
    End Try
    End Sub
    >
    >
    >

  • Raymond Chiu

    #2
    Re: Excel object cannot released without error

    Dear Scotte,

    Then How can I do?
    I have tried to the following.But still no work.
    Is there anything I miss. If not using this way to open excel, is there any
    other way to open?

    dim objwb as excel.workbook
    objwb=excel.wor kbooks.open(pat h)


    then add the following after worksheet and before excel.

    objwb.close
    releasecom(objw b)
    objwb=nothing
    "Scott M." <s-mar@nospam.nosp amwrote in message
    news:%23TAcMUb4 IHA.3500@TK2MSF TNGP05.phx.gbl. ..
    Many methods return objects when they are called. With Excel these objects
    are placed in memory and must be destroyed via ReleaseComObjec t as you
    have done with your NAR method, but the line:
    >
    objExcel.Workbo oks.Open(Page.M apPath("reports \BLCost.xls"))
    >
    could be the culprit since the Open method returns a reference to a
    Workbook object that you haven't assigned a variable to. Since you have
    no variable to explicitly use to destroy the reference, the reference
    counter for that object can't get to zero and therefore Excel won't close.
    >
    You should always explicitly assign a variable to all references, that
    means any method or property call that returns an object.
    >
    -Scott
    >
    "Raymond Chiu" <rchiu5hk@yahoo .com.hkwrote in message
    news:edZvvCZ4IH A.1204@TK2MSFTN GP04.phx.gbl...
    >Dear All,
    >I have the problem which exists for several months and cannot find the
    >solution in the web, I see others also have the same situation but they
    >are also not solved.
    >>
    >It is excel object which still remain in the process in the task manager
    >in the server. Another project does not have this problem and they are
    >on the same server. I have tried to change from win2000 server to win2003
    >server, from ms office 2003 to ms office 2007. Excel still remain in the
    >process in the task manager. Although it would not affect another excel
    >running of another records, I need to clear the excel session everyday in
    >the server.
    >>
    >You can try using the attached excel file to see whether it would cause
    >the same problem in your computer. Please Let me know and your ideas.
    >Appreciate for your help!!!!
    >>
    >---------------------coding -----------------------------------------------------------------------------------
    >>
    > Dim objExcel As New Excel.Applicati on
    > Dim objSheet As Excel.Worksheet
    >>
    > objExcel.Workbo oks.Open(Page.M apPath("reports \BLCost.xls"))
    >>
    > objExcel.Displa yAlerts = False
    > objSheet = objExcel.Sheets ("Form")
    > objSheet.Copy(A fter:=objSheet)
    >>
    > objSheet = objExcel.Active Sheet
    >>
    >.............. ............... ............... ...........
    >>
    > objExcel.Sheets ("Form").Delete ()
    >>
    > objSheet.SaveAs (Page.MapPath(" ..\out\" & sFileName))
    >>
    > NAR(objSheet)
    > objExcel.Workbo oks.Close()
    > NAR(objExcel.Wo rkbooks)
    > objExcel.Quit()
    > NAR(objExcel)
    > GC.Collect()
    >>
    >---------------end of
    >sub-----------------------------------------------------------------
    >>
    >Private Sub NAR(ByVal o As Object)
    > Try
    > System.Runtime. InteropServices .Marshal.Releas eComObject(o)
    >>
    > Catch ex As Exception
    > Dim remarklabel As Label
    > remarklabel = CType(FormView1 .FindControl("r emarklabel"),
    >Label)
    > remarklabel.Tex t = remarklabel.Tex t + ex.ToString()
    > Finally
    > o = Nothing
    > End Try
    > End Sub
    >>
    >>
    >>
    >
    >

    Comment

    Working...