View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter Huang Peter Huang is offline
external usenet poster
 
Posts: 115
Default hang when formula bar open

Hi,

As I said before, when we are in the edit mode, we can not know which excel
childwindow has the input focus, because in this time all the automation
code will hang. If you really want to do this, I think you may try to
simulate the press the escape key to cancel the editor mode. But this is
not recomnended and the method may be ugly.

Here goes the code, just for your reference.

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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetFocus1 Lib "user32" Alias "SetFocus" (ByVal
hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
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 Declare Sub CopyMemory Lib "kernel32.dll" _
Alias "RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type

Private Type INPUT_TYPE
dwType As Long
xi(0 To 23) As Byte
End Type

Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const VK_SHIFT = &H10
Private Const VK_HOME = &H24
Private Const VK_END = &H23
Private Const INPUT_KEYBOARD = 1

Private Declare Function MapVirtualKey Lib "user32" _
Alias "MapVirtualKeyA" (ByVal wCode As Long, _
ByVal wMapType As Long) As Long

Private Declare Function SendInput Lib "user32.dll" _
(ByVal nInputs As Long, pInputs As INPUT_TYPE, _
ByVal cbsize As Long) As Long

Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const VK_RIGHT = &H27
Const VK_RETURN = &HD
Const VK_ESCAPE = &H1B
Private Sub Command1_Click()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim w As Excel.Window
App.OleServerBusyRaiseError = True
App.OleServerBusyTimeout = 1000
App.OleServerBusyMsgText = ""
Set wb = WebBrowser1.Document
On Error GoTo ErrorHandler
Debug.Print wb.Application.ActiveWindow.Caption
Exit Sub
ErrorHandler:
Debug.Print Err.Description
Call Command3_Click
wb.Application.Cells(1, 1) = "c"
End Sub

Private Sub Command3_Click()
Dim hwnd As Long
hwnd = FindWindow("ThunderFormDC", vbNullString)
hwnd = FindWindowEx(hwnd, 0, "Shell Embedding", vbNullString)
SetFocus1 hwnd
Dim wVkKey(1) As Integer
Dim UpDown(1) As Integer
wVkKey(0) = VK_ESCAPE: UpDown(0) = 0
wVkKey(1) = VK_ESCAPE: UpDown(1) = 1
sKeyEventSet 2, wVkKey, UpDown
End Sub

Private Sub Form_Load()
WebBrowser1.Navigate2 "file:///C:/test.xls"
End Sub
Private Sub sKeyEventSet(nInput As Long, _
wVkKey() As Integer, UpDown() As Integer)
Dim inputevents() As INPUT_TYPE
Dim keyevent As KEYBDINPUT
Dim Count As Integer
ReDim inputevents(nInput - 1) As INPUT_TYPE
For Count = 0 To nInput - 1
With keyevent
.wVk = wVkKey(Count)
.wScan = MapVirtualKey(wVkKey(Count), 0)
If UpDown(Count) = 0 Then
.dwFlags = KEYEVENTF_EXTENDEDKEY Or 0
Else
.dwFlags = KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP
End If
.time = 0
.dwExtraInfo = 0
End With
inputevents(Count).dwType = INPUT_KEYBOARD
CopyMemory inputevents(Count).xi(0), keyevent, Len(keyevent)
Next Count
Dim rt As Long
rt = SendInput(nInput, inputevents(0), Len(inputevents(0)))
Debug.Print rt
Debug.Print Err.LastDllError
End Sub



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.