开发者

VSTO excel addin gets unloaded after button click event is handled

开发者 https://www.devze.com 2023-01-09 01:29 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消