在活动 window 中访问自定义任务窗格 - Visual Basic、VSTO

Accessing custom task pane in active window - Visual Basic, VSTO

我正在 VSTO 中为 Ppt 2013 创建 COM 加载项,但在引用活动 window 中的自定义任务窗格时遇到问题。

我的代码应该使自定义任务窗格仅对活动 window 可见,但它当前对所有文档 windows.

运行

我的代码是:

For Each CTP As Microsoft.Office.Tools.CustomTaskPane In Globals.ThisAddIn.CustomTaskPanes

        If CTP.Window Is Globals.ThisAddIn.Application.ActiveWindow Then
            CTP.Visible = True
        End If

    Next

任务窗格添加到使用以下代码创建/打开的每个新演示文稿

AddIn_control1 = New AddIn_control
AddIn_taskpane = Me.CustomTaskPanes.add(AddIn_control1, "Add-in taskpane", Me.Application.ActiveWindow)

我做了一个小实验,发现 CustomTaskPane.Window 总是 ActiveWindow。因此,要解决此问题,您可以在某些字典中继续跟踪大头钉:

Dictionary<CustomTaskPane, PowerPoint.Presentation> ctpDict = new Dictionary<CustomTaskPane, PowerPoint.Presentation>();
void Application_AfterNewPresentation(PowerPoint.Presentation Pres) {
    AddIn_control AddIn_control1 = new AddIn_control();
    CustomTaskPane AddIn_taskpane = this.CustomTaskPanes.Add(AddIn_control1, "Add-In Taskpane", this.Application.ActiveWindow);
    ctpDict.Add(AddIn_taskpane, Pres);
}

稍后您可以使用它:

if (cptDict[CTP] == Globals.ThisAddIn.Application.ActivePresentation) {
    CTP.Visible = true;
}