问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/15688982/excel-vsto-workbooks-open-only-working-when-another-action-is-taken-first