Using Excel Files

impel123

New member
Joined
Jan 9, 2010
Messages
3
Programming Experience
Beginner
hello,

if i wont to work with an excel file in vb .net, what i should do to close correctly all opened variables?

i decrare some variables:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet

then, i read and wite data to the excel file.

when i finished what code should i use to deactivate any bindings with the excel file??

I'm asking because when i finish,if i try to open the excel file it says that file is using by somebody and i cant modify it,despite i have my application closed.

thnx
 
VB.NET:
book.Close()
xlApp.Quit()
 
Hi,

I know exactly what you mean, Excel just doesn't like closing...

This maybe overkill but here is what I did to ensure the whole thing closed out when I had to work with Excel recently:
VB.NET:
Public Sub CloseXL( ByRef oXL as Excel.Application)
    Dim XLWB As Excel.Workbook
    Dim XLWBS As Excel.Workbooks

    'close active workbook
    XLWB = oXL.ActiveWorkBook
    XLWB.Close(False) 'saved elsewhere
    ReleaseActiveX(XLWB)
    XLWB = nothing

    'close any other workbooks
    XLWBS = oXL.Workbooks
    XLWBS.Close()
    ReleaseActiveX(XLWBS)
    XLWBS = nothing

    oXL.Quit()
    ReleaseActiveX(oXL)
    oXL = nothing
End Sub

Public Sub ReleaseActiveX(ByRef o as Object)
    Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
    Catch ex As Exception
    Finally
            o = Nothing
    End Try
End Sub

A bit extreme I'll admit but it did close down the Excel! :rolleyes:
 
Back
Top