处理按钮点击事件后,VSTO excel插件将被卸载

时间:2010-07-23 09:10:15

标签: vba excel-vba visual-studio-2008 com vsto

我为excel创建了一个插件,其中有一个功能区和一个按钮。我已使用以下代码处理了按钮单击事件

 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:\\Test.xls")

End Sub

当调用返回时,应用程序打开工作簿,但是在打开它之后,它会进一步取消添加并调用ThisAddIn_Shutdown方法。任何人都可以帮我找出我出错的地方,如何停止卸载excel插件?

1 个答案:

答案 0 :(得分:0)

我认为你的问题就在这里。

Globals.ThisAddIn.Application.Workbooks.Close()

在Excel中,Application.Workbooks.Close将关闭所有工作簿。这包括你的插件。您需要指定要关闭的工作簿。例如:

activeWorkbook.Close

Application.Workbooks("Test.xls").Close