2
我为Excel(2016)做了一个AddIn,它将打印区域保存为jpg文件。它在几个月内运行良好。现在我收到越来越多的用户错误报告,都会得到相同的错误(下图)。用户使用Excel 2013的Windows 7或使用Excel 2016的Windows 10时,都出现此错误。在第一次重新安装我的程序帮助,但从现在起它没有帮助。GetDataObject请求剪贴板操作不成功0x800401D0
这里是我的代码:
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);
}
}
您是否正在非STA线程上运行? (PS:异常的类型可以严重帮助...)也检查此:https://stackoverflow.com/questions/68666/clipbrd-e-cant-open-error-when-setting-the-clipboard-from- net –
@SimonMourier是的,我在非STA线程上运行它。我将错误消息附加到了我的问题,这是我的全部,因为我无法在我的计算机上重现此错误。 – Adam
您必须使用STA线程来访问剪贴板。 –