C# Microsoft.Office.Interop.Excel.Range.CopyPicture works on some computers but not on others

1.5k views Asked by At

I'm trying to copy a picture of the selected Excel Range to Clipboard and show that Bitmap on my GUI. I have this method to get the Bitmap:

using Excel = Microsoft.Office.Interop.Excel;

public static Bitmap CopyPicture(ref Excel.Range range)
    {
        System.Threading.Thread.Sleep(1000);    //Google told me...
        range.Select();                         //...that these lines are possible fixes.
        range.Copy();                           //They aren't.
        try
        {
            range.CopyPicture(Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }

        IDataObject data = Clipboard.GetDataObject();
        if (data != null)
        {
            if (data.GetDataPresent(DataFormats.Bitmap))
            {
                Bitmap map = (Bitmap)data.GetData(DataFormats.Bitmap, true);
                MessageBox.Show("Return map");
                return map;
            }
        }
        MessageBox.Show("Return null");
        return null;
    }

The problem is, that on my computer (which I use to develop) and on one other computer this solution works just fine, and I get the picture that I wanted. On some other computers it doesn't work, the range.CopyPicture(...) throws this COMException.

All these computers have the latest update of Excel and are running Windows 7. There's no difference when using different Users (admin or not).

All help is appreciated, I've been stuck with this couple of days now.

0

There are 0 answers