473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Using Excel from Threads

I am trying to access Excel spreadsheets to modify data from a VB.NET
application. If I have an object defined as:

Public Class Test
Private _excel As Microsoft.Office.Interop.Excel.Application
Public Property ExcelApp() As
Microsoft.Office.Interop.Excel.Application
Get
Return _excel
End Get
Set(ByVal Value As Microsoft.Office.Interop.Excel.Application)
_excel = Value
End Set
End Property
Public Sub Process()
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
do somethings here before using excel
oBook = _excel.Workbooks.Open("myfile.xls")
do something with oBook
End Sub
End Class

and access like:
Dim otest As New Test
otest.ExcelApp = New Microsoft.Office.Interop.Excel.Application
otest.Process()

This works fine. But if I try to use a thread like:
Dim otest As New Test
otest.ExcelApp = New Microsoft.Office.Interop.Excel.Application
Dim t As New System.Threading.Thread(AddressOf otest.Process)
t.Start()

the code dies. I'm tracing it through with the debugger. The code
gets to the line:
oBook = _excel.Workbooks.Open("myfile.xls")
and when I press F10 and it never comes back.
Is there an issue with using threads and Excel? I've tested using
just one thread so I don't think it is a deadlock issue.

Thanks for any help,

Steve

Mar 5 '07 #1
2 2010
>Is there an issue with using threads and Excel?
Basically you should only use the Excel objects from the same thread
they were created on, and that thread should be an STA thread (see the
Thread.ApartmentState property).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 5 '07 #2
On Mar 5, 4:12 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
Is there an issue with using threads and Excel?

Basically you should only use the Excel objects from the same thread
they were created on, and that thread should be an STA thread (see the
Thread.ApartmentState property).

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Thanks. Opening Excel in the thread and using the STA thread seemed
to solve the problem (now I just need to decide if the overhead of
opening excel multiple times is worth using threads for this).

Steve

Mar 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sylvia | last post by:
Dear All, Would appreciate if someone can help me out on this. I have an Excel Add-in which is only available on the web server. The Add-in allows user to retrieve data from a proprietary...
1
by: s_cnot | last post by:
I wanted to use a new Thread to open Excel.Application in my web project and it gave access denied. If I do not use a new thread, i.e NO thread.start(), then this Excel.Application instantiation is...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
1
by: Rick Brown | last post by:
After reading 30+ threads on the subject and implementing pertinent changes I still have an instance of Excel that won't close. I hope its due to my poor coding and someone can spot the error....
13
by: aleksandra_83 | last post by:
Hello, I have searched all over google groups and internet, but I have not found a solution to this problem that has worked. The solutions I found helped me single out the line that is causing...
5
by: Bob Calvanese | last post by:
I have a main form (frmMain). frmMain creates a sub form (frmSub). frmSub creates a business object (busObj). busObj creates a thread and calls a method to run in the thread. The method creates a...
5
by: Graham | last post by:
I have been asked by a client to produce a proof of concept of how .Net performs and works within an Excel workbook. Everything works perfectly with all the required events firing and data being read...
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
5
by: jenniferhelen | last post by:
I have been searching threads for a while and found the instructions listed below many times, however when I get to step 6 and select to save, I always receive the following error, "The information...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.