0

I'm not super familiar with what exactly the STAThread does (is), so I'm not sure if my question is even properly stated.

I have a process running in a BackgroundWorker thread that copies the contents of an Excel range to the clipboard (Excel.Range.Copy()). After the copying, I need to be able to access the Clipboard contents to write them into a text file, but apparently the Clipboard cannot be accessed directly from my BackgroundWorker (when I try to use Clipboard.GetText() from my BackgroundWorker and write that text into my textfile, no text is passed, even though I can manually do Ctrl-V on a separate text file and paste the contents that have just been copied from the Excel range by the C# process).

I should also mention that I'm running this process in a BackgroundWorker to facilitate the use of a ProgressBar that shows the process status. So, if there's a solution that let's me use my ProgressBar and access the Clipboard contents without using a BackgroundWorker, I absolutely welcome it! Thanks!

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
sergeidave
  • 662
  • 4
  • 11
  • 23
  • Possible duplicate of http://stackoverflow.com/questions/7635994/getting-data-off-the-clipboard-inside-a-backgroundworker. If not, there's a great solution in there related to your question. – villecoder Jan 28 '13 at 23:03

1 Answers1

0

Here is some thing that's pretty cool, and simple. Just invoke the UI thread, assuming your project is WinForms based, then you can interact with Microsoft products. In our case we used Outlook, but Excel works like this too:

BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(this.bgw_DoWork);
bgw.RunWorkerAsync();

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    // Invoke the UI thread
    // "this" refering to the Form1, or what ever your form is
    this.Invoke((MethodInvoker)delegate
    {
        MsOutlook.ApplicationClass OutlookApplication = new MsOutlook.ApplicationClass();
        // do whatever here ... etc etc

    }
}
Conrad de Wet
  • 477
  • 6
  • 15