C# excel addin如何实现Ribbon上一个按钮显示一个UserControl?
发布网友
发布时间:2022-05-16 17:36
我来回答
共1个回答
热心网友
时间:2023-11-07 10:56
得把uc 添加到CustomTaskPaneCollection 中才行
给你一个参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools;
namespace FinWordHelper
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentOpen += Application_DocumentOpen;
this.Application.DocumentChange += Application_DocumentChange;
}
void Application_DocumentChange()
{
FinPaneManager.Instance.CollectWordPanes();
}
void Application_DocumentOpen(Word.Document Doc)
{
FinPaneManager.Instance.CollectWordPanes();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO 生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
#region FinPaneManager
public class FinPaneManager
{
private FinPaneManager()
{
custPanes = Globals.ThisAddIn.CustomTaskPanes;
}
private static FinPaneManager m_Instance = new FinPaneManager();
public static FinPaneManager Instance
{
get { return m_Instance; }
}
private CustomTaskPaneCollection custPanes;
public CustomTaskPane GetFinWordFormatPane()
{
Word.Window window = Globals.ThisAddIn.Application.ActiveWindow;
foreach (CustomTaskPane ctp in custPanes)
{
if (ctp.Title == "Fin格式化工具" && ctp.Window == window)
{
return ctp;
}
}
var cp = custPanes.Add(
new WordFormatter(),
"Fin格式化工具",
window
);
cp.Width = 250;
return cp;
}
public CustomTaskPane GetFinWordCheckPane()
{
Word.Window window = Globals.ThisAddIn.Application.ActiveWindow;
foreach (CustomTaskPane ctp in custPanes)
{
if (ctp.Title == "拼写检查" && ctp.Window == window)
{
return ctp;
}
}
var cp = custPanes.Add(
new WordSpellCheck(),
"拼写检查",
window
);
cp.Width = 250;
return cp;
}
public void CollectWordPanes()
{
for (int i = custPanes.Count - 1; i >= 0; i--)
{
if (custPanes[i].Window == null)
{
custPanes.RemoveAt(i);
}
}
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
namespace FinWordHelper
{
public partial class CustomRibbon //按钮调用在这里
{
private void CustomRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
FinPaneManager.Instance.GetFinWordFormatPane().Visible = true;
}
private void button2_Click(object sender, RibbonControlEventArgs e)
{
FinPaneManager.Instance.GetFinWordCheckPane().Visible = true;
}
}
}