VSTO excel addin gets unloaded after button click event is handled
I have created a addin for excel, where there is a ribbon and a button on it. I have handled the event of button click with the following code
Private Sub test_button_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles test_button.Click
Dim activeWorksheet As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet
Dim str As String
Dim activeWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim sheet As Excel.Worksheet
Dim sheet_name As String
Globals.ThisAddIn.Application.Workbooks.Open("c:\\Test.xls")
str = Globals.ThisAddIn.Application.ActiveWorkbook.FullName
activeWorkbook.Save()
Globals.ThisAddIn.Application.Workbooks.Close()
'Call to python com object
Dim PythonUtils = CreateObject("PythonDemos.Utilities")
Dim response = PythonUtils.SplitString("Hello from VB", str)
MsgBox(response)
Globals.ThisAddIn.Application.Workbooks.Open("c:开发者_如何学Go\\Test.xls")
End Sub
When the call returns, the application open the workbook, but then after opening it, it futher proceeds to unlode the adding and the ThisAddIn_Shutdown method is called. Can anyone please help me find out as to where i am going wrong and how can i stop unloading of the excel addin?
I think your problem is this line right here.
Globals.ThisAddIn.Application.Workbooks.Close()
In Excel, Application.Workbooks.Close will close ALL workbooks. This includes your addin. You need to specify which workbook you are closing. For example:
activeWorkbook.Close
or
Application.Workbooks("Test.xls").Close
精彩评论