根据在功能区 VSTO 上打开的工作簿文件设置选项卡的可见性 Excel
Set visibility of tab based on workbook file opened on ribbon VSTO Excel
我有一个 VSTO 应用程序,在 MainRibbon.xml 我正在使用 getVisible 处理程序来确定该选项卡是否应该可见。在 MainRibbon.cs 中调用了方法 setVisbility。如果 filename/workbook openend 被调用 "Template.xlsm",我试图将选项卡的可见性设置为 true。否则我不想显示下面的标签。
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load" >
<ribbon >
<tabs>
<tab idMso="TabAddIns" label="M Template" getVisible="setVisbility" >
<group id="CreateLoadModel"
label="Create/Load Model">
<button id="createmodelbutton" label="Create New Model"
screentip="Text" onAction="OnCreateModel"
supertip="Create a new Model"
imageMso="GroupSmartArtCreateGraphic"/>
<button id="loadmodelbutton" label="Load Existing Model"
screentip="Text" onAction="OnLoadModel"
supertip="Load an Exisitng Model"
imageMso="FileOpen"/>
</group>
public bool setVisbility(Office.IRibbonControl control)
{
var name = Globals.ThisAddIn.Application.ActiveWorkbook.FullName;
if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
{
return false;
}
else
{
return true;
}
}
我目前 运行 应用程序 'Globals.ThisAddIn.Application.ActiveWorkbook.Name ' 的所有内容都是未定义的 NULL 对象。这是因为 excel 应用程序打开时没有单击要打开的文件。但是,当我单击必要的文件时 - 它会返回到此 setVisibility 方法,但它仍然指出未定义对象。如何根据我打开的 excel 工作簿将可见性设置为 true/false?
出现此问题是因为 setVisibility
在加载工作簿之前是 运行。即加载项加载时 setVisibility
将 运行。
一个解决方案是使用 Workbooks.Count
检查是否加载了任何工作簿,然后在每次激活工作簿时更新可见性:
public bool setVisbility(Office.IRibbonControl control)
{
int nWorkbooks = Globals.ThisAddIn.Application.Workbooks.Count;
if (nWorkbooks == 0)
{
return false;
}
if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
{
return true;
}
else
{
return false;
}
}
然后在您的 ThisAddIn_StartUp 方法中为激活的工作簿添加一个事件处理程序:
this.Application.WorkbookActivate += Application_WorkbookActivate;
然后当激活工作簿时强制功能区进行验证,如:
private void Application_WorkbookActivate(Workbook Wb)
{
RibbonClass.RibbonInstance.Invalidate()
}
取决于您对源文件的命名。
我有一个 VSTO 应用程序,在 MainRibbon.xml 我正在使用 getVisible 处理程序来确定该选项卡是否应该可见。在 MainRibbon.cs 中调用了方法 setVisbility。如果 filename/workbook openend 被调用 "Template.xlsm",我试图将选项卡的可见性设置为 true。否则我不想显示下面的标签。
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load" >
<ribbon >
<tabs>
<tab idMso="TabAddIns" label="M Template" getVisible="setVisbility" >
<group id="CreateLoadModel"
label="Create/Load Model">
<button id="createmodelbutton" label="Create New Model"
screentip="Text" onAction="OnCreateModel"
supertip="Create a new Model"
imageMso="GroupSmartArtCreateGraphic"/>
<button id="loadmodelbutton" label="Load Existing Model"
screentip="Text" onAction="OnLoadModel"
supertip="Load an Exisitng Model"
imageMso="FileOpen"/>
</group>
public bool setVisbility(Office.IRibbonControl control)
{
var name = Globals.ThisAddIn.Application.ActiveWorkbook.FullName;
if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
{
return false;
}
else
{
return true;
}
}
我目前 运行 应用程序 'Globals.ThisAddIn.Application.ActiveWorkbook.Name ' 的所有内容都是未定义的 NULL 对象。这是因为 excel 应用程序打开时没有单击要打开的文件。但是,当我单击必要的文件时 - 它会返回到此 setVisibility 方法,但它仍然指出未定义对象。如何根据我打开的 excel 工作簿将可见性设置为 true/false?
出现此问题是因为 setVisibility
在加载工作簿之前是 运行。即加载项加载时 setVisibility
将 运行。
一个解决方案是使用 Workbooks.Count
检查是否加载了任何工作簿,然后在每次激活工作簿时更新可见性:
public bool setVisbility(Office.IRibbonControl control)
{
int nWorkbooks = Globals.ThisAddIn.Application.Workbooks.Count;
if (nWorkbooks == 0)
{
return false;
}
if (Globals.ThisAddIn.Application.ActiveWorkbook != null &&
Globals.ThisAddIn.Application.ActiveWorkbook.Name == "Template.xlsm")
{
return true;
}
else
{
return false;
}
}
然后在您的 ThisAddIn_StartUp 方法中为激活的工作簿添加一个事件处理程序:
this.Application.WorkbookActivate += Application_WorkbookActivate;
然后当激活工作簿时强制功能区进行验证,如:
private void Application_WorkbookActivate(Workbook Wb)
{
RibbonClass.RibbonInstance.Invalidate()
}
取决于您对源文件的命名。