做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;
}
}
理解后,很容易就解决了这个问题,分享一下