Insert Bitmap into a Word Document into a thread  
Author Message
Greg.B





PostPosted: Visual C# General, Insert Bitmap into a Word Document into a thread Top

Hi everybody,
I use a 'BackgroundWorker' to generate a word document in background. I want to insert a bitmap which is generate on the fly.
If I use this method (that work outside the thread) :

System.Drawing.Bitmap bitmap;
[..]
System.Windows.Forms.Clipboard.SetDataObject(bitmap);
[..]
Microsoft.Office.Interop.Word.Range range;
[..]
range.Paste();

I have an 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.

How can I set the backgroundworker to be 'STA' Is there an another way to proceed


Thanks by advance.

Greg


Visual C#14  
 
 
Markku Behm





PostPosted: Visual C# General, Insert Bitmap into a Word Document into a thread Top

Hi

If it's posible, save bitmap to the temporaty file, insert it to word document and then delete file.

Yours Markku


 
 
Greg.B





PostPosted: Visual C# General, Insert Bitmap into a Word Document into a thread Top

Tanks for your reply, it works now with :

[..]
bitmap.Save(Path.GetTempPath() + "[..]");
[..]
range.InlineShapes.AddPicture([..], ref missing, ref missing, ref missing);


 
 
ahmedilyas





PostPosted: Visual C# General, Insert Bitmap into a Word Document into a thread Top

in your program.cs file, make sure that static void Main() has [STAThread] attribute:

[STAThread]

static void Main()

{

Application.Run(new Form1()); //whatever the form is

}



 
 
Uwe Keim





PostPosted: Visual C# General, Insert Bitmap into a Word Document into a thread Top

No, this is not a solution. I do have [STAThread], but since doing it from a background thread, it doesn't seem to work that simple.

Probably we need to post a message to the main thread to handle it.