Outlook add-ins - Error accessing Clipboard

D

Dilum

I've developed an Outlook add-in using VSTO 2005 and Office 2003 and C#. I've
added a new command bar with some buttons.

I need to get text from Clipboard to the message body upon clicking of one
of my buttons. I'm using hte following code.

if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
mailItem.Body =
Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
else
mailItem.Body = "The clipboad does not contain any text\\links";

But 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 funciton has STAThreadAttribute
marked on it."

I tried putting [STAThread] attribute to methods and even created a new
thread and put [STAThread] to its target method. But still the error message
comes.

Is there any way to solve this issue.

Thanking you
Dilum
 
K

Ken Slovak - [MVP - Outlook]

I've had better luck using the Win32 Clipboard API calls from user32, like
GetClipboardData(). I've never had to set STA threading when doing that.
 
D

Dilum

Hi Ken,

Thank you very much for your response. I appreciate it very much. I
tried for some sample on the Internet how to do this but I couldnt find a
relevant content.

I just want to get text data from the clipboard to a String variable. Also
how will we able to know the ids of data formats usesd with
"EnumClipboardFormats". I mean, how can we determine the id for text, id for
Html etc so that we can get the appropriate content. Im using C#.

Thank you
Dilum

Ken Slovak - said:
I've had better luck using the Win32 Clipboard API calls from user32, like
GetClipboardData(). I've never had to set STA threading when doing that.




Dilum said:
I've developed an Outlook add-in using VSTO 2005 and Office 2003 and C#.
I've
added a new command bar with some buttons.

I need to get text from Clipboard to the message body upon clicking of one
of my buttons. I'm using hte following code.

if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
mailItem.Body =
Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
else
mailItem.Body = "The clipboad does not contain any text\\links";

But 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 funciton has STAThreadAttribute
marked on it."

I tried putting [STAThread] attribute to methods and even created a new
thread and put [STAThread] to its target method. But still the error
message
comes.

Is there any way to solve this issue.

Thanking you
Dilum
 
K

Ken Slovak - [MVP - Outlook]

Well, I can't post the entire clipboard code I use since it consists of 4
different classes. I did google for clipboard and c# and found a number of
usable code samples however.

To enumerate the formats you'd do something like this:

uint format = 0;
while ((format = EnumClipboardFormats(format)) != 0)
{
string formatName = "0";
if (format > 14)
{
StringBuilder sb = new StringBuilder();
if (GetClipboardFormatName(format, sb, 100) > 0)
{
formatName = sb.ToString();
}
}
}

Less than 14 are the built-in formats.
 
D

Dilum

Hi Ken,

I got through it. Thank you very much for your help. I appreciate it
very much.

Thank you
Dilum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top