通过 VSTO 将控件添加到 Excel 工作簿会重置 VBA 中的所有内容
我有一个 VSTO Excel Addin 解决方案,它通过 ComVisible 接口向 VBA 公开某些方法。请参阅此获取示例实现。我为此使用 VSTO 4 和 Excel 2007/2010。 从 VBA 调用时,公开接口的某些方法会插入控件(按钮、组合框或 Winform UserControl)。该方法正确执行并将控件插入到工作表上。
public void AddButtonControl(string strControlName)
{
Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(nativeWorksheet);
vstoWorksheet.Controls.AddButton(nativeWorksheet.Range["A1", "E5"], strControlName);
}
现在,一旦将控件插入到工作簿中,VBA 中声明的所有变量都会重置。所有代码都会返回到状态,就好像它从未被执行过一样。
我的解决方案和 Excel 工作簿在此处
共享构建解决方案并打开工作簿 SampleTest.xlsm 单击“检查字符串值”,将显示一个消息框,显示“默认文本”。 单击按钮 - “动态添加按钮”。将在工作表顶部插入一个按钮。将显示一条消息 - “将文本分配给字符串:示例文本”。 现在再次单击“检查字符串值”。它应该显示“示例文本”,但显示了一个空白消息框。
欢迎任何建议/解决方法。
I have a VSTO Excel Addin solution which exposes certain methods to VBA through a ComVisible interface. Please refer this for a sample implementation. I am using VSTO 4 and Excel 2007/2010 for this.
Certain methods of the exposed interface insert a control (button, combo box or a Winform UserControl) when called from VBA. The method executes properly and inserts the control on the sheet as well.
public void AddButtonControl(string strControlName)
{
Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(nativeWorksheet);
vstoWorksheet.Controls.AddButton(nativeWorksheet.Range["A1", "E5"], strControlName);
}
Now as soon as the control is inserted in the workbook, all the variables declared in VBA are reset. All code goes back to the state as if it were never executed.
My solution and an Excel workbook are shared here
Just build the solution and open the workbook SampleTest.xlsm
Click "Check string value", a message box saying "Default Text" would be displayed.
Click on button - "Add Button Dynamically". A button would be inserted at the top of the sheet. and a message saying - "Assigning text to string: Sample Text" would be displayed.
Now click "Check string value" again. It should have displayed "Sample Text" but a blank message box is displayed.
Any suggestions/workarounds are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论