Excel VSTO Workbook。仅在首先执行其他操作时才打开

时间:2013-03-28 18:00:47

标签: vb.net excel vsto add-in ribbon

我正在开发VSTO加载项。我有一个自定义功能区,在该功能区上有一个名为TemplateCallButton的按钮。我还有其他几个功能和按钮,其中一个只打开一个带有模板的文件夹(作为示例包含在内)。 TemplateCallButton仅在其他操作之一完成时才会起作用并添加到模板文件中(看似无关紧要)。在任何其他操作运行后,它按预期工作。

更令人沮丧的是,这种行为似乎只发生在我部署的机器上,而不是我正在开发的机器上。这是TemplateCallButton代码:

Public Class InsightLabProcessor
  Dim MainTemplatePath As String = "C:\Insight\Insight.xltm"
  ....
  Private Sub TemplateCallButton_Click(sender As Object, e As RibbonControlEventArgs) Handles TemplateCallButton.Click
    Dim objApp As Excel.Application
    objApp = Marshal.GetActiveObject("Excel.Application")
    objApp.Visible = True
    Dim objWorkbook As Excel.Workbook = objApp.Workbooks.Open(MainTemplatePath)
    objWorkbook.Worksheets(4).Activate()
  End Sub

以下是只打开文件夹的按钮的代码:

Private Sub PhaseCodeFolderOpenButton_Click(sender As Object, e As RibbonControlEventArgs) Handles PhaseCodeFolderOpenButton.Click
    Process.Start("explorer.exe", "C:\Insight\Phase Codes")
End Sub

或打开控制表格的那个:

Private Sub ControlPannel_Click(sender As Object, e As RibbonControlEventArgs) Handles ControlPannel.Click
    Dim controlpanel As New ControlPanel
    controlpanel.Show()
    controlpanel = Nothing
End Sub

我觉得我必须遗漏一些简单的东西。

感谢。

1 个答案:

答案 0 :(得分:0)

所以问题实际上是这里提到的问题:http://support.microsoft.com/kb/238610,作为一个加载项处理似乎很恶毒。我发现的最好的解决方案(再次不是很优雅)就是打开命令行,写出我们正在等待第一个实例加载,在任何人都太好奇之前关闭它。我在4台机器上尝试了这个,经验上发现我需要的最长等待时间是250毫秒,所以我把它加倍到500:

    ...
    Shell("C:\Windows\System32\cmd.exe", AppWinStyle.MaximizedFocus)
    System.Threading.Thread.Sleep(10) 'give cmd just a bit to take the line
    SendKeys.Send("Waiting for Excel to register in Running Object Table")
    System.Threading.Thread.Sleep(490)
    Dim Term() As Process = Process.GetProcessesByName("cmd")
    For Each P As Process In Term
        P.Kill() 'user probably doesn't have any other instances of cmd open, if they do they are colaterial damage, may handle that if it becomes an issue
    Next
    Dim objApp As Excel.Application
    objApp = Marshal.GetActiveObject("Excel.Application")
    objApp.Visible = True
    Dim objWorkbook As Excel.Workbook = objApp.Workbooks.Open(MainTemplatePath)
    objWorkbook.Worksheets(4).Activate()

我会再次回来,除了最终用户更优雅或更可接受的任何东西。我真的很想知道是否有办法强制Excel注册到ROT。也许我应该把它变成另一个问题。