Solved

VB.NET 2003 Excel Interop call hangs when repeated

Posted on 2007-03-30
7
527 Views
Last Modified: 2010-05-18
In my VB.NET 2003 application, Excel is spawned using this:
Imports Excel = Microsoft.Office.Interop.Excel
: which is assigned outside of the formMain class.

This works fine the first time Excel is called.  But as soon as I try to run the data display subroutine (which called Excel the first time) the program freezes.  This happens even if Excel isn't going to be used in that subroutine, which is what's really confusing.  The subroutine starts but does not appear to process even a single line of code before locking up.

So examples:
1) Run data display with "tables" option instead of "excel" - works fine
2) Run with "tables" - works fine
3) Run with "excel" - works fine
4) Run with "excel" - hangs

1) Run with "excel" - works fine
2) Run with "excel" - hangs

1) Run with "excel" - works fine
2) Run with "tables" - hangs

This is very, very confusing to me.  Any help?

0
Comment
Question by:richdiesal
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:vjc2003
ID: 18834505
looks a bit confusing too.
are you properly initialising and closing excel in the subroutine??

check in process tab of TaskManager if u are exiting the Excel application.
Else when u initialise it again,program will freeze.

Also are you initialising excel in some common place?
0
 
LVL 9

Author Comment

by:richdiesal
ID: 18838265
All I have referencing Excel in a common place is outside of everything:
Imports Excel = Microsoft.Office.Interop.Excel

The rest is inside an If clause of a subroutine:
            Dim oXL As New Excel.Application
            If oXL Is Nothing Then Throw (New Exception("Fatal Error: Excel launch failed."))
            Dim oBook As Excel.Workbook
            Dim oSheet As Excel.Worksheet
            'oXL = New Excel.Application
            oBook = oXL.Workbooks.Add()
            oBook = oXL.ActiveWorkbook

..... some Excel parsing here...

            oBook.SaveAs(SaveText.FileName, Excel.XlFileFormat.xlWorkbookNormal)
            oXL.Quit()

            oSheet = Nothing
            oBook = Nothing
            oXL = Nothing


You are right about Excel still being open, but shouldn't oXL.Quit take care of that?  Or is something else wrong here?
0
 
LVL 2

Expert Comment

by:vjc2003
ID: 18841580
Also add this to cleanup part and see if it helps:
oWorkBook.Close(false,null,null)
oXL.Workbooks.Close()

also are'nt u doing some exception handling?
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 9

Author Comment

by:richdiesal
ID: 18847332
I added the above - did not help.

And additional exception handling... no.  What you see above is everything I'm doing (as it relates to opening/closing Excel).  I'm really more of a web development in PHP sort of developer (this is a side-project), so this is somewhat uncharted territory for me...
0
 
LVL 2

Accepted Solution

by:
vjc2003 earned 500 total points
ID: 18849076
Maybe you can try  this:
Kill the excel process manually and see if it is locking up again.

which user account are you using to run the application.
Hope you have given proper permissions to the account.
0
 
LVL 9

Author Comment

by:richdiesal
ID: 18852137
Your prompting me to see if Excel was even open when the problem occurred caused me to investigate options OTHER than Excel, and it turns out there was another error with an infinite While loop that I hadn't noticed before.  All fixed now.  Thanks!
0
 
LVL 2

Expert Comment

by:vjc2003
ID: 18855596
Grt,we were on the wrong lane altogether.
actually,I am a newbie in vb.net too.
Good to know ur problem is solved and thanks for the points.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question