c# - 如何在excel VTO插件中重新创建按钮
问题描述
我有这段代码,当我按下功能区上的按钮时,它会被激活
Microsoft.Office.Tools.Excel.Worksheet vstoDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);
Excel.Range selection = Globals.ThisAddIn.Application.Selection as Excel.Range;
if (selection != null)
{
Button button = new Button();
button.Text = "THIS IS A BUTTON";
vstoDocument.Controls.AddControl(button, selection, name);
button.Click += new EventHandler(btn_Click);
// save the button state and name in custom properties
Excel.Worksheet sheet = Application.ActiveSheet as Excel.Worksheet;
sheet.CustomProperties.Add("btnName" "btn_1");
sheet.CustomProperties.Add("text","THIS IS A BUTTON");
sheet.CustomProperties.Add("selection", selection);
}
//the click event
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("I got Clicked");
}
所以当我按下ribbion上的按钮时这有效问题是当我保存excel并再次打开它时范围没有在我的启动代码中分配
Excel.Application wb;
wb = this.Application;
if (wb.ActiveCell.Count > 0)
{
object docIndex = 1;
Worksheet vstoDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);
foreach (Excel.CustomProperty prop in props)
{
if (prop != null)
{
string pn = prop.Name;
if (pn.Contains("selection"))
{
Excel.Range selection = prop.Value as Excel.Range;
if (selection != null)
{
Button button = new Button();
vstoDocument.Controls.AddControl(button, selection, "btn_1");//get an error here
button.Click += new EventHandler(btn_Click);
button.Text = "THIS IS A BUTTON";
button.TextAlign = ContentAlignment.MiddleCenter;
}
}
}
}
我有一个错误说
System.OutOfMemoryException HResult=0x8007000E Message=没有足够的内存资源来完成此操作。(来自 HRESULT 的异常:0x8007000E (E_OUTOFMEMORY))来源 = StackTrace:
解决方案
推荐阅读
- python - 在每个值之间添加 N 个空行
- python - Cartopy:循环多个范围而不重新生成地图图像/删除网格线对象
- javascript - 在 javascript 中,是否可以简写将所有导入放在一个数组中以进行导出?
- python - 使用双 for 循环计算列的平均值
- mysql - SQL查询中的结转/结转余额
- visual-studio-code - 如何在vscode中格式化像golang struct这样的graphql模式?
- git - GitHub Actions:结帐操作下载“损坏/超大”文件
- java - ArrayList 相对于 LinkedList 的优势以及它们之间的性能比较
- azure - 如何检索本地 Service Fabric 群集的证书指纹?
- machine-learning - 您好,关于 sklearn.Pipeline 的两个问题,带有用于时间序列的自定义转换器