C#Microsoft.Office.Interop.Excel.Range.CopyPicture适用于某些计算机但不适用于其他计算机

时间:2017-08-28 11:25:24

标签: c# excel office-interop comexception

我正在尝试将所选Excel范围的图片复制到剪贴板,并在我的GUI上显示该位图。我有这个方法来获取位图:

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;
    }

问题是,在我的计算机上(我用它来开发)和另一台计算机上的这个解决方案工作正常,我得到了我想要的图片。在其他一些计算机上它不起作用,range.CopyPicture(...)抛出了这个 COMException

所有这些计算机都有Excel的最新更新,并且正在运行Windows 7.使用不同的用户(管理员与否)没有区别。

所有的帮助表示赞赏,我现在已经被困住了几天。

0 个答案:

没有答案