473,785 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Offic e.Interop.Excel .Application
Public Property ExcelApp() As
Microsoft.Offic e.Interop.Excel .Application
Get
Return _excel
End Get
Set(ByVal Value As Microsoft.Offic e.Interop.Excel .Application)
_excel = Value
End Set
End Property
Public Sub Process()
Dim oBook As Microsoft.Offic e.Interop.Excel .Workbook
do somethings here before using excel
oBook = _excel.Workbook s.Open("myfile. xls")
do something with oBook
End Sub
End Class

and access like:
Dim otest As New Test
otest.ExcelApp = New Microsoft.Offic e.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.Offic e.Interop.Excel .Application
Dim t As New System.Threadin g.Thread(Addres sOf otest.Process)
t.Start()

the code dies. I'm tracing it through with the debugger. The code
gets to the line:
oBook = _excel.Workbook s.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 2022
>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.Apartmen tState 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.w ant.s...@mvps.o rg>
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.Apartmen tState property).

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/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
1771
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 database. I have configured an Excel workbook to retrieve the data via the Add-in base on some inputs that the user enter in some cells (B7 and B8) in sheet1.
1
1309
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 fine. Any ideas ? Thanks
11
4057
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 again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000 for me) I of course thought that I mustn't be unloading a variable properly.
1
3554
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. I've used Dev Avish's code as a starting point and the Sub does what I want less the hanging instance. Please help if you can, Rick Public Sub Email_Supplier() Dim dbs As DATABASE
13
5973
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 the problem, though. I programmatically open a Microsoft Excel file from Access, do some data checks etc., define a named range, perform DoCmd.TransferSpreadsheet, then delete the range, set all referenced objects to Nothing, etc. However, Excel...
5
1576
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 server side buiness object (busObjSrv). busObjSrv creates an Excel.Application (exAp) object on the server. exAp opens an automated excel woekbook (exWb) (automated by an outside company) busObjSrv fills the information from frmSub on exWb and...
5
1581
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 from the correct cells. I sent this to the client and he is happy but asked some questions, and Im not too sure on the answers (he is an old school VBA developer at heart and doesn't know anything about .Net): ...
19
1877
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='". $file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='". $file_name."&order_by=location'>Location</a></th> <th><a href='". $file_name."&order_by=sighting_date_asc'>Sighting
5
12587
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 cannot be pasted because the copy area and the paste area are not the same size and shape". Does this function only work if the number or rows and number of columns are the same number? Does anyone have any suggestions for me? I greatly appreaciate...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.