我在这里做教程。使用空白的Excel页面,一切正常
https://msdn.microsoft.com/en-us/library/bb608590(v=vs.120).aspx
当我加载excel表时,有人给了我,然后点击toggleButton1来显示我得到的窗格
{“任务窗格已被删除或不再有效。”}
就行了
private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}
指向该任务窗格的指针是否会以某种方式消失?
Microsoft.Office.Tools.CustomTaskPane PartPhotoTaskPane;
Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;
void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
{
if (PartPhotoTaskPane != null)
{
PartPhotoTaskPane.Dispose();
InitalizePartPhotoViewerTaskPane(EPPF);
}
else
{
InitalizePartPhotoViewerTaskPane(EPPF);
}
}
/// <summary>
/// Start up the part photo viewer task pane
/// </summary>
private void InitalizePartPhotoViewerTaskPane(ExcelPartPhotoFunctions _EPPF)
{
//intialize the part search
try
{
PartPhotoTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new PartPhotoSearchPane(_EPPF), "Part Information", Globals.ThisAddIn.Application.ActiveWindow);
PartPhotoTaskPane.Visible = Properties.Settings.Default.InfoPaneOpenStatus;
PartPhotoTaskPane.Width = 260;
}
catch (Exception e)
{
MessageBox.Show("Error starting Part Info Toolbar:" + Environment.NewLine +
e.Message + Environment.NewLine + e.StackTrace, "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
ATTEMPT 1:
点击事件仍然出错。我猜这是需要在课程之间共享该窗格的相关内容吗?
private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}
private void ExcelEvents_WorkbookActivate(Excel.Workbook wb)
{
var taskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
if (taskPane == null)
{
UcCenlarInvest control = new UcCenlarInvest();
taskPane = this.CustomTaskPanes.Add(control, "My pane for workbook " + wb.Name);
customTaskPanes[new WeakReference(wb)] = taskPane;
}
}
ATTEMPT 2:
所以现在Ribbon类可以获得TaskPane但我仍然得到相同的错误。补充一点:
private CustomTaskPane taskPane;
public CustomTaskPane TaskPane
{
get
{
//return (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
return pane;
}
set
{
taskPane = value;
}
}
.....
TaskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
答案 0 :(得分:5)
Excel 2016是一个单一文档界面(SDI),Excel的单个实例中的每个工作簿都包含自己的功能区UI。 more information
如果打开新工作簿,则会出现一个新功能区,但指向任务窗格的指针将丢失。如果没有任何任务窗格,则应实现WorkbookActivate事件并添加新任务窗格。您还应该保留指向自定义任务窗格的指针的静态列表。
请参阅此解决方案:CustomTaskPane in Excel doesn't appear in new Workbooks
答案 1 :(得分:0)
您也可以通过检查任务窗格控件的IsDisposed
属性来解决此问题,例如
if (taskPane.CustomTaskPane?.Control.IsDisposed == false)
{
taskPane.CustomTaskPane.Visible = false;
}