Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to develop a word Add-in(my first one). I get the following error: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

In the following line:
System.Windows.Forms.Clipboard.SetText(pressedKey);



Now I completely understand the error but I can't find the Main function of ThisAddIn. I know this sounds stupid but, please help me.
Posted
Comments
BillWoodruff 25-Oct-14 16:41pm    
Since I have not done this myself in the context of making an Office interop add-in, I post this as a comment, not a solution.

If you locate the place where the thread is created and started, and do this:

Thread newThread = new Thread(...);
newThread.ApartmentState = ApartmentState.STA;
newThread.Start();

does that work ?
Tejas Shastri 25-Oct-14 16:51pm    
var thread = new Thread(() => { //MessageBox.Show(pressedKey);
if(pressedKey.Equals("Space")||pressedKey.Equals("Tab"))
{
System.Windows.Forms.Clipboard.SetText(pressedKey);
IDataObject key = Clipboard.GetDataObject();
string mykey = key.GetData(DataFormats.Text).ToString();
MessageBox.Show(mykey);
}
});
new Thread.ApartmentState = ApartmentState.STA;
new Thread.Start();

Gives me error:
Error 1 A new expression requires (), [], or {} after type (this is for the line :newThread.ApartmentState = ApartmentState.STA;)

Error 2 'System.Threading.Thread.ApartmentState' is a 'property' but is used like a 'type'
Error 3 'System.Threading.Thread.Start(object)' is a 'method' but is used like a 'type'
BillWoodruff 25-Oct-14 19:26pm    
Your code for constructing a new Thread has errors: see:

http://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.100).aspx

It also makes no sense to me to create a new Thread with a delegate that essentially invokes 'MessageBox.

However, as I said, I am not familiar with this context (add-in for Office), and, sorry, I can't be more helpful than what I've already said.
Tejas Shastri 26-Oct-14 2:57am    
I'm using a thread because it takes too much time for the messagebox to show up.. response time is faster with a thread..
Tejas Shastri 26-Oct-14 3:02am    
Thanks a lot for the link bill :) I fixed it.. :)

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