【问题标题】:How to properly dispose / release PowerPoint.DocumentWindow如何正确处置/释放 PowerPoint.DocumentWindow
【发布时间】:2015-01-12 07:23:08
【问题描述】:

问题:如何处理父PPT进程创建的子Excel窗口?

  1. 我将图表从 Excel 复制到 Power Point 2013
  2. 然后我用鼠标右键单击此图表并选择“编辑数据”
  3. 将出现紧凑的 Excel 弹出窗口,其中包含此图表的源数据
  4. 我关闭了这个窗口并且无法再次打开它,因为这个窗口的句柄似乎没有正确释放

如果Excel窗口中的某些数据发生变化,这是用于更新PPT图表的代码。

protected void UpdateSlide()
{
    using (PresentationWrapper presentation = ParentSlide.Parent)
    using (DocumentWindowWrapper window = presentation.Windows[0]) // !!! here is the place where Com oject is maintained but needs to be released
    {
        window.Activate();
    }
}

这是不释放 Com 对象的包装器。

public class DocumentWindowWrapper : ComWrapper<ppt.DocumentWindow>, IWindow
{
    DocumentWindowWrapper wrapper;

    public static DocumentWindowWrapper CreateWrapper(ppt.DocumentWindow wrappedObject)
        wrapper = new DocumentWindowWrapper(wrappedObject);
        return wrapper;
    }

    public override void Dispose()
    {
        Marshal.ReleaseComObject(...); // !!! what should I put here to release this window?
        base.Dispose();
    }
}

这就是我说的窗口。

【问题讨论】:

  • 正如 Yegor 所暗示的,presentation.Windows[0] 创建了两个 .NET 包装器(WindowsDocumentWindow)并且只发布了一个。也许这是你的问题?

标签: c# excel vsto powerpoint


【解决方案1】:

应使用 ReleaseComObject 方法释放底层 COM 对象 (wrappedObject)。

【讨论】:

    【解决方案2】:

    代码应如下所示:

    开始使用工作表:

    Worksheets sheets = excelApp.Worksheets; 
    Worksheet sheet = sheets.Open(...);
    

    然后释放(它们总是以创建的相反顺序释放):

    Marshal.ReleaseComObject(sheet);
    Marshal.ReleaseComObject(sheets);
    

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多