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.
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);
}
}
All clipboard access must run using an STA thread. There are many ways to do this:
User contributions licensed under CC BY-SA 3.0