Private Sub Command1_Click()
Call SwitchToAppMarkedAs("App2", "C:\Temp\myApp2.accdb")
oAccessRemoteApp.Run "DisplayMessage", "This is app 2 from app 1. Hello there"
End Sub
Public Sub SwitchToAppMarkedAs(strAppMarkedAs As String, strAppPathAndFileName As String)
Dim bolFound As Boolean
Dim lngHWND As Long
Dim iid As GUID
Dim obj As Object
Dim lngReturn As Long
' Minimize the current window
ShowWindow Application.hWndAccessApp, SW_SHOWMINIMIZED
' Find if the app is loaded, and if not, start it
If AppIsLoaded(strAppMarkedAs) = False Then
' Start the application - we can use shell or automation. I'll use automation
Set oAccessRemoteApp = CreateObject("Access.Application")
oAccessRemoteApp.UserControl = True
oAccessRemoteApp.OpenCurrentDatabase (strAppPathAndFileName)
' Optionally run a macro or do something else here.
' oAccessRemoteApp.Run "<macro name>"
End If
' Now switch to the app.
' Find the existing instance(it may have already been running) and switch to it.
bolFound = False
Do While bolFound = False
lngHWND = GetWindow(GetDesktopWindow(), GW_HWNDChild)
Do While lngHWND > 0
If GetProp(lngHWND, strAppMarkedAs) = 1 Then
BringWindowToTop (lngHWND)
lngReturn = ShowWindow(lngHWND, 3)
bolFound = True
lngHWND = 0
Else
lngHWND = GetWindow(lngHWND, GW_HWNDNEXT)
End If
Loop
DoEvents
Loop
Exit_Procedure:
End Sub
' Make sure we have an object reference to it.
If oAccessRemoteApp Is Nothing Then
Call IIDFromString(StrPtr(IID_IDispatch), iid)
If AccessibleObjectFromWindow(lngHWND, OBJID_NATIVEOM, iid, obj) = S_OK Then
Set oAccessRemoteApp = obj
End If
End If
Public Sub SwitchToAppMarkedAs(strAppMarkedAs As String, strAppPathAndFileName As String)
Dim bolFound As Boolean
Dim lngHWND As Long
Dim iid As GUID
Dim obj As Object
Dim lngReturn As Long
' Minimize the current window
ShowWindow Application.hWndAccessApp, SW_SHOWMINIMIZED
' Find if the app is loaded, and if not, start it
If AppIsLoaded(strAppMarkedAs) = False Then
' Start the application - we can use shell or automation. I'll use automation
Set oAccessRemoteApp = CreateObject("Access.Application")
oAccessRemoteApp.UserControl = True
oAccessRemoteApp.OpenCurrentDatabase (strAppPathAndFileName)
' Optionally run a macro or do something else here.
' oAccessRemoteApp.Run "<macro name>"
End If
' Now switch to the app.
' Find the existing instance(it may have already been running), make sure
' we have a reference to it, and switch to it.
bolFound = False
Do While bolFound = False
lngHWND = GetWindow(GetDesktopWindow(), GW_HWNDChild)
Do While lngHWND > 0
If GetProp(lngHWND, strAppMarkedAs) = 1 Then
' Make sure we have an object reference to it.
If oAccessRemoteApp Is Nothing Then
Call IIDFromString(StrPtr(IID_IDispatch), iid)
If AccessibleObjectFromWindow(lngHWND, OBJID_NATIVEOM, iid, obj) = S_OK Then
Set oAccessRemoteApp = obj
End If
End If
BringWindowToTop (lngHWND)
lngReturn = ShowWindow(lngHWND, 3)
bolFound = True
lngHWND = 0
Else
lngHWND = GetWindow(lngHWND, GW_HWNDNEXT)
End If
Loop
DoEvents
Loop
Exit_Procedure:
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Great article. I use a similar process in a procedure I use for reading from or writing to Excel. The problem is that occassionally, the user might want to read from or write to a workbook which is already open, and if the user has more than one instances of Excel open it is vertually impossible to select the correct version. With a method very similar to this, I am able to identify all available instances of Excel and allow the user to select the appropriate instance based on the workbook(s) opened in those instances.
Dale