Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,

I have a function that opens about 40 excel spreadsheet in vb.net. It opens them one by one then extracts the data in them and writes it to a SQL table.

It works perfectly, although slow. But, I can only surmise that because the process is so slow it seems to the OS (windows 7) that the excel book has stopped responding and then pops a dialog on the screen to say that the application has stopped responding. It goes away again once the excel book is closed and the object are disposed.

I have checked the code and it seems to be pretty standard where ever I look. I added the withevents clause to the excel application class to try and see if that won't help.

Is there a way to suppress these messages?

'globel excel vars
Dim WithEvents xlsLookup As Excel.Application
Dim xlsWorkbook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet

'Create excel objects
xlsLookup = New Excel.ApplicationClass
xlsWorkbook = xlsLookup.Workbooks.Open(ShippingHistoryFilename)
xlsSheet = xlsWorkbook.Worksheets(1)

'reading happens here...

Finally
            'Remove the table from the data set
            If MAIN.data.IsTableLoaded("ShippingHistory") = True Then MAIN.data.DS.Tables.Remove("ShippingHistory")
            If Not IsNothing(xlsWorkbook) Then
                xlsWorkbook.Close()
                xlsLookup.Quit()
                releaseObject(xlsLookup)
                releaseObject(xlsWorkbook)
                releaseObject(xlsSheet)
            End If
        End Try
Posted
Comments
Tarun.K.S 12-Nov-10 9:10am    
how about trying this: xlApp.DisplayAlerts=false

I did a search on vb.net windows7 not responding and almost all the hits I got seemed to indicate that the solution was to have the long-running code on a separate thread.
 
Share this answer
 
Hi,

Thanks for your info.

I have tried, but still getting the message. It might be caused by something else though as I am getting all sorts of cross threading errors to the progress bars and textboxes on the actual form.

not sure what will happen with the data row and data table objects.

I will play around a bit and feed back progress.

Dim StufThread As New System.Threading.Thread(AddressOf Me.Import_StuffPack) 'Import all the stuffinglist excel docs 
Dim PickThread As New System.Threading.Thread(AddressOf Me.Import_Packlist)' import all the packinglist excel docs

If ShippingHistory_Update() = True Then
       StufThread.Start()
       'PickThread.Start() ' Will implement later
        End If
 
Share this answer
 
Comments
Henry Minute 12-Nov-10 9:53am    
To resolve your cross-threading errors you should google for 'invokerequired'. There are lots of articles out there, so have a read.
Replace this code

VB
If Not IsNothing(xlsWorkbook) Then
  xlsWorkbook.Close()
  xlsLookup.Quit()
  releaseObject(xlsLookup)  
  releaseObject(xlsWorkbook) 
  releaseObject(xlsSheet) 
End If


with this

VB
releaseComObject(xlsSheet)
xlsWorkbook.Close(False)
releaseComObject(xlsWorkbook)
xlsLookup.Quit()
releaseComObject(xlsLookup)
'optional
GC.Collect()
GC.WaitForPendingFinalizers()

VB
'<a href="http://support.microsoft.com/kb/317109">http://support.microsoft.com/kb/317109</a>[<a href="http://support.microsoft.com/kb/317109" target="_blank" title="New Window">^</a>]
Private Sub releaseComObject(ByVal o As Object)
  Try
    System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
  Catch
  Finally
    o = Nothing
  End Try
End Sub


1) Note the sequence of releasing object references.

2) Globals are never recommended, especially with COM references and debugging. You should open Excel, use it and release it. The performance hit is minimal compared to managing COM references.

3) Starting a new process thread will cause all sorts of trouble because it will have no concept of your user interface thread and can only communicate via invokes. Multithreading is a deep subject and rarely offers much of a performance gain in typical .net applications.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900