GetDataObject Requested Clipboard operation did not succeed 0x800401D0

2

I made an AddIn for Excel (2016), that saves the print area as a jpg file. It worked fine for a few months. Nowadays I get more and more error reports from users, all get the same error (picture below). The users have Windows 7 with Excel 2013 or Windows 10 with Excel 2016, both have this error. At the first times reinstall of my program helped, but from now it doesn't help.

enter image description here

Here's my code:

public static void Save(string report, string area, RibbonControlEventArgs e)
{
    Excel.Window window = e.Control.Context;
    Excel.Worksheet sheet = ((Excel.Worksheet)window.Application.ActiveSheet);
    Excel.Range range = sheet.Range[sheet.PageSetup.PrintArea];

    range.CopyPicture(Excel.XlPictureAppearance.xlPrinter, Excel.XlCopyPictureFormat.xlPicture);
    range.Copy(Type.Missing);

    string fileName = @"\\server.company.lan\report.jpg";

    if (Clipboard.GetDataObject() != null)
    {
        IDataObject data = Clipboard.GetDataObject();

        Image image = (Image)data.GetData(DataFormats.Bitmap, true);
        image.Save(fileName, ImageFormat.Jpeg);
    }
}
c#
excel
vsto
clipboard
excel-addins
asked on Stack Overflow Aug 3, 2017 by Adam

1 Answer

3

All clipboard access must run using an STA thread. There are many ways to do this:

  • use BeginInvoke functions (Winforms or WPF) that guarantee the code runs on the UI thread (which should be STA)
  • start your own Thread instance and use SetAparmentState(STA),
  • etc.
answered on Stack Overflow Sep 8, 2017 by Simon Mourier

User contributions licensed under CC BY-SA 3.0