Excel VSTO Workbooks.Open仅在首先采取其他操作时才能工作 [英] Excel VSTO Workbooks.Open only working when another action is taken first

查看:297
本文介绍了Excel VSTO Workbooks.Open仅在首先采取其他操作时才能工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VSTO加载项。我有一个自定义的功能区,在该功能区上有一个名为 TemplateCallButton 的按钮。我还有几个其他功能和按钮,其中一个只是打开一个带有模板的文件夹(以示例为例)。如果其他动作已经完成,那么<​​code> TemplateCallButton 只能在模板文件中工作并添加(似乎无关紧要)。在任何其他操作运行之后,它将按预期工作。



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

 公共类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)处理PhaseCodeFolderOpenButton 。
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





谢谢。

解决方案

div>

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

  ... 
Shell(C:\Windows\System32\cmd.exe,AppWinStyle.MaximizedFocus)
System.Threading.Thread.Sleep(10)'给cmd只是一个位
SendKeys.Send(等待Excel在运行对象表中注册)
System.Threading.Thread.Sleep(490)
Dim Term()As Process = Process.GetProcessesByName(cmd)
对于每个P作为过程在条款
P.Kill()'用户可能没有任何其他cmd的实例打开,如果它们是重大损害,可以处理这个问题
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).Activ ate()

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


I am working on a VSTO add-in. I have a customized ribbon, and on that ribbon a button called TemplateCallButton. I also have several other functions and buttons, one of which just opens a folder with templates (included as example). The TemplateCallButton only works and adds in a template file if one of the other actions has been completed (seemingly doesn't matter which one). After any other action has run then it works as expected.

What's more frustrating is that this behavior only seems to happen on machines I deploy on, and not the one I'm developing on. Here is the TemplateCallButton code:

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

and here is the code for the button that just opens a folder:

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

or one that opens the control form:

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

I feel like I must be missing something simple.

Thanks.

解决方案

So the problem is in fact the one addressed here: http://support.microsoft.com/kb/238610, which seems pretty vicious to deal with as an add-in. The best solution I've found (again not very elegant) is to just open the command line, write out that we're waiting for the first instance to load, the close it before anyone get's too curious. I tried this on 4 machines and empirically found the longest wait time I needed was 250 ms, so I doubled it to 500 in this:

    ...
    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()

Again I would come back and except anything that was more elegant or acceptable to an end user. I would really love to know if there was a way to force Excel to register to the ROT. Perhaps I should turn that into another question.

这篇关于Excel VSTO Workbooks.Open仅在首先采取其他操作时才能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆