We help IT Professionals succeed at work.
Get Started

Genericizing Open-Instance hunting in Excel or Access: late binding for Access instances?

Last Modified: 2012-09-06
My question is a followon to the superb answer I received on a previous question: https://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27828791.html.  The attached code from  Corey2 completely resolved my requirements, and I quickly realized the need to do the same thing for Access instances.  My existing code returns an array of all the running instances of Excel.  By parameterizing one or more key bits, the IID_IDispatch being the most obvious example, I'm sure I can genericize the functionality in short order to choose between an array of either Excel or Access applications.

Thanks for the excellent work, and I look forward to taking this to the next level.  For your conveience, I've included my top-level code block that, except for returning a viable list of open instances, is a verbatim copy of the previous submission.

~Peter Ferber

    Public Function Render_ExcelApps() As Microsoft.Office.Interop.Excel.Application()
        Dim Return_ExcelApps() As Microsoft.Office.Interop.Excel.Application

        Dim iCount As Integer

        iCount = -1
        Return_ExcelApps = Nothing
        For Each p As Process In Process.GetProcessesByName("Excel")
            Dim App As ExcelWindow
                ' Walk the children of this window to see if any are
                ' IAccessible.
                Dim hwnd As Integer = p.MainWindowHandle
                Dim hWndChild As Integer = 0
                Dim cb As EnumChildCallback = New EnumChildCallback(AddressOf EnumChildProc)
                EnumChildWindows(hwnd, cb, hWndChild)
                ' OBJID_NATIVEOM gets us a pointer to the native 
                ' object model.
                Dim OBJID_NATIVEOM As UInteger = CUInt("&HFFFFFFF0")
                Dim IID_IDispatch As Guid = GetType(Excel.Application).GUID
                IID_IDispatch = New Guid("{00020400-0000-0000-C000-000000000046}") '

                App = Nothing
                Dim hr As Integer = AccessibleObjectFromWindow(hWndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), App)
                If (Not App Is Nothing) Then
                    iCount += 1
                    ReDim Preserve Return_ExcelApps(iCount)
                    Return_ExcelApps(iCount) = App.Application
                    '                    Debug.Print(hr)
                End If
            Catch ex As Exception

            End Try

        Next p
        Render_ExcelApps = Return_ExcelApps

    End Function

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE