Avatar of alainbryden
Flag for Canada

asked on 

Wrapped COM application (Excel) not responding to input without a double click. Visual Basic .NET

I have built a control which wraps an excel application into my own application over the past month or so. I've met many challenges, but have solved all of them until now.

A picture of my application is attached.

As you can see, Excel has the illusion of being smoothly integrated into my excel application.

The problem is that the application will not receive input of any kind until it has been double clicked.

I've attempted to capture every event the program fires. All of the activate and deactivate events (there are 8 different ones in total) only fire when the file is first opened and closed, but do not fire when focus shifts from my application to the excel window or any other window on the computer. The one event that does fire repeatedly is the xlApp.SheetSelectionChange event. Thus if they select a cell on the worksheet (other than the one last selected), I have a chance to run some code and try to get the application ready for input.

I've tried every form of input, but unless a double click event has been triggered in the excel application, it will not notice any key being pressed, or the mouse scroll wheel. I can click around all I want, but no other form of input will work until a double click has been triggered, not even automated input - xlApp.Sendkeys has no effect and SendKeys.Send has no effect on the application.

Can anyone think of any ways of making this application work normally. The likely solution will require some way of sending an automated double click event at the co-ordinates that the user clicked, followed by the escape key ( xlApp.SendKeys("{ESC}") ) so that the user is none the wiser, and doing so in the SheetSelectionChange event.

Thank you. I've attached some code to help start things off.

Alain Bryden
'Once upon a time'
Public Function OpenApp()
    Private WithEvents xlApp As Excel.Application
    xlApp = New Excel.Application
    Private appHWnd As Integer = xlApp.Hwnd
    SetParent(CType(appHWnd, IntPtr), Me.Handle)
    'The following hack (courtesy of Microsoft) is supposed to solve the initial UI unresponsiveness'
    'But even after sending these two keys, the double click is required each time excel loses focus' 
    SendKeys.Send(" ")
    'To make the excel window fit so nicely into the custom control (called ExcelHoster):'
    Private Sub ExcelHoster_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Dim Swidth As Integer = SystemInformation.Border3DSize.Width
        Dim Sheight As Integer = SystemInformation.Border3DSize.Height
        If Not xlApp Is Nothing Then _
            MoveWindow(appHWnd, -2 * Swidth, 0 * Sheight - Cheight - Theight, _
                       Me.Width + 4 * Swidth, Me.Height + Cheight + 2 * Sheight + Theight, True)
    End Sub
'This event will likely need to solve my problem:'
    Private Sub UserClicked() Handles xlApp.SheetSelectionChange
        'xlApp.SendKeys("{F2}") ''Useless'
        'Code required to resume normal UI functionality (i.e. double click event fire)'
        xlApp.SendKeys("{ESC}") 'Useless until double click event has fired'
    End Sub

Open in new window

System ProgrammingMicrosoft Excel.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon