码迷,mamicode.com
首页 > 其他好文 > 详细

VSTO PowerPoint 代码删除Shape后再恢复出现无法再次获取的问题

时间:2016-07-24 20:37:46      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:

做PowerPoint的VSTO插件项目,遇到个很奇怪的问题,当代码执行删除某些Shape时,没问题,但是操作Undo也就是恢复后,无法再次获取到之前删除的对象,这种情况只在Office2007中出现,亲测2010没问题。

就在快要放弃的时候,终于看到了这个

传送门:https://www.add-in-express.com/creating-addins-blog/2014/06/24/exception-hresult-0x800a01a8/

关键内容搬运如下:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    PowerPoint.DocumentWindow activeWindow = PowerPointApp.ActiveWindow;
    if (activeWindow != null) {
        PowerPoint.View view = activeWindow.View;
        if (view != null) {
            object obj = view.Slide;
            if (obj != null) {
                if (obj is PowerPoint.Slide) {
                    PowerPoint.Shapes shapes = (obj as PowerPoint.Slide).Shapes;
                    if (shapes.HasTitle == Microsoft.Office.Core.MsoTriState.msoTrue) {
                        PowerPoint.Shape titleShape = shapes.Title;
                        object titleParent = null;
                        try {
                            titleParent = titleShape.Parent;
                        } catch (Exception ex) {
                            MessageBox.Show(ex.Message);
                        }
                        if (titleParent != null) {
                            // use titleParent
                        }
                        titleShape.Delete();
                        // leaving the COM object below unreleased causes 0x800A01A8 if you click Undo in the PP UI and invoke this code anew
                        Marshal.ReleaseComObject(titleShape); titleShape = null;
                    }
                    Marshal.ReleaseComObject(shapes); shapes = null;
                }
                Marshal.ReleaseComObject(obj); obj = null;
            }
            Marshal.ReleaseComObject(view); view = null;
        }
        Marshal.ReleaseComObject(activeWindow); activeWindow = null;
    }
}

理解后,很容易就解决了这个问题,分享一下

VSTO PowerPoint 代码删除Shape后再恢复出现无法再次获取的问题

标签:

原文地址:http://www.cnblogs.com/jiushiliu/p/5701386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!