[COLOR=seagreen]'////////////////////////////////////////////////////////[/COLOR]
[COLOR=seagreen]'Routine that checks if a target instance of Excel[/COLOR]
[COLOR=seagreen]'is currently in edit mode and if so,force it[/COLOR]
[COLOR=seagreen]'to exit the edit mode in time, to make it capable[/COLOR]
[COLOR=seagreen]'of responding to automation calls from the current app[/COLOR]
[COLOR=seagreen]'without code causing exceptions and crashing.[/COLOR]
[COLOR=seagreen]'////////////////////////////////////////////////////////[/COLOR]
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function IsIconic Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GWL_STYLE As Long = -16
Private Const WS_VISIBLE As Long = &H10000000
Private Const VK_ESC As Long = &H1B
Private Const WM_KEYDOWN As Long = &H100
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOWNA = 8
Private Const SW_HIDE = 0
Function MakeSureApplicationIsReady _
(ServerApplication As Application) As Boolean
Dim lXLDESKhwnd As Long, lXL6hwnd As Long
Dim lCurrentStyle As Long
[COLOR=seagreen]'/make sure the server app is a different excel instance.[/COLOR]
If Application.hwnd <> ServerApplication.hwnd Then
[COLOR=seagreen]'/if so,get its workbooks desktop hwnd.[/COLOR]
lXLDESKhwnd = FindWindowEx _
(ServerApplication.hwnd, 0, "XLDESK", vbNullString)
[COLOR=seagreen]'/find the servers's editor window hwnd.[/COLOR]
lXL6hwnd = FindWindowEx _
(lXLDESKhwnd, 0, "EXCEL6", vbNullString)
[COLOR=seagreen]'/now retrieve servers's editor current window styles.[/COLOR]
lCurrentStyle = GetWindowLong(lXL6hwnd, GWL_STYLE)
[COLOR=seagreen]'/check if the visible style is set-[/COLOR]
[COLOR=seagreen]'/meaning the app is in edit mode.[/COLOR]
If lCurrentStyle And WS_VISIBLE Then
Debug.Print "Edit mode."
[COLOR=seagreen]'/if the server app is in edit mode,exit the edit mode[/COLOR]
[COLOR=seagreen]'/by sending to it a discreet 'esc' key stroke without[/COLOR]
[COLOR=seagreen]'/activating it.[/COLOR]
With ServerApplication
Select Case True
Case IsIconic(.hwnd)
ShowWindow .hwnd, SW_HIDE
ShowWindow .hwnd, SW_SHOWNOACTIVATE
PostMessage .hwnd, WM_KEYDOWN, VK_ESC, 0
ShowWindow .hwnd, SW_HIDE
ShowWindow .hwnd, SW_SHOWNA
Case Else
PostMessage .hwnd, WM_KEYDOWN, VK_ESC, 0
End Select
End With
Else
Debug.Print "Ready mode."
End If
[COLOR=seagreen]'/return True to indicate edit mode is now cancelled and[/COLOR]
[COLOR=seagreen]'/automation can now be safely performed on the server app.[/COLOR]
MakeSureApplicationIsReady = True
End If
End Function