0

OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))'

I'm trying to put a text in an excel file (using Interop).

     String EE = "Some random text here";
            Thread thread1 = new Thread(() => Clipboard.SetText(EE));
            thread1.SetApartmentState(ApartmentState.STA); //Set the thread to STA
            thread1.Start();
            thread1.Join(); //Wait for the thread to end

            Microsoft.Office.Interop.Excel.Range CR1 = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[3, 1];
            CR1.Select();
            xlWorkSheet.Paste(CR1, false);

The error is coming from the second line. The problem here is that this error is not coming each time I run the code. If I click the button that runs the code, sometimes I get an error, and sometimes I don't, just like that. What could be the issue?

Zkai
  • 211
  • 1
  • 3
  • 8
  • 1
    Possibly relevant: https://stackoverflow.com/questions/68666/clipbrd-e-cant-open-error-when-setting-the-clipboard-from-net – jpw Aug 18 '18 at 08:53
  • 1
    I think the error indicates that clipboard cant be opened atm and its normal behavior. You should add a retry logic. – NullReference Aug 18 '18 at 08:55
  • 1
    You may find some useful suggestions here: https://stackoverflow.com/questions/12769264/openclipboard-failed-when-copy-pasting-data-from-wpf-datagrid – Ruud Helderman Aug 18 '18 at 09:02

0 Answers0