vbatinker
05-27-2010, 04:48 AM
Hi everyone, I am new to the forum so let me know if I am posting this issue in the wrong place. I am trying to send an enter key press in VBA from Excel 2007 to a webpage in internet explorer 8 programmatically. Below is sample code referencing the web page that when you press the enter key, that keypress info will show up in the textarea of the webpage.
I originally set it up by using the SendKeys function in coordination AppActivate and this works. However, it works intermittently especially since I need to run this overnight and my machine has other processes running.
I would prefer to send the enter key press using sendmessage (or postmessage) API function from the Win API. I believe this would eliminate the intermittent issues I am seeing, and not require me to bring the ie8 window to the top. However, IE 8 doesn't seem to be detecting the keycode I am sending it. I am sure I am doing something wrong here. Please let me know if you need more information.
Thanks in advance.
Public ie As Object
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Public Const WM_GETTEXT = &HD
Private Const WM_CHAR = &H102
Private Const VK_RETURN = &HD 'Enter Key
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SETTEXT = &HC
Private Const WM_USER As Long = &H400
Sub openpage(url As String)
ie.navigate url
waitForLoad
End Sub
Sub waitForLoad()
Do
If Not ie.Busy And ie.ReadyState = 4 Then
DoEvents
Application.Wait (Now + TimeValue("00:00:01"))
If Not ie.Busy And ie.ReadyState = 4 Then
Exit Do
End If
End If
DoEvents
Loop
End Sub
Sub TestSendMessageShort()
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
openpage "http://unixpapa.com/js/testkey.html"
waitForLoad 'function that checks if document loading is complete
Set varInput = ie.document.getElementById("t")
varInput.Value = "This is a text string"
varInput.Focus
'--------------- This piece of code doesn't work
SendMessage ie.hwnd, WM_KEYDOWN, VK_RETURN, 0
SendMessage ie.hwnd, WM_KEYUP, VK_RETURN, 0
'--------------- The Below piece of code works (intermittently) ----------
AppActivate ie.document.Title, True
Application.Wait (Now + TimeValue("00:00:01"))
SendKeys "{ENTER}", True
DoEvents
Application.Wait (Now + TimeValue("00:00:02"))
'ie.Quit
Set ie = Nothing End Sub
I originally set it up by using the SendKeys function in coordination AppActivate and this works. However, it works intermittently especially since I need to run this overnight and my machine has other processes running.
I would prefer to send the enter key press using sendmessage (or postmessage) API function from the Win API. I believe this would eliminate the intermittent issues I am seeing, and not require me to bring the ie8 window to the top. However, IE 8 doesn't seem to be detecting the keycode I am sending it. I am sure I am doing something wrong here. Please let me know if you need more information.
Thanks in advance.
Public ie As Object
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Public Const WM_GETTEXT = &HD
Private Const WM_CHAR = &H102
Private Const VK_RETURN = &HD 'Enter Key
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SETTEXT = &HC
Private Const WM_USER As Long = &H400
Sub openpage(url As String)
ie.navigate url
waitForLoad
End Sub
Sub waitForLoad()
Do
If Not ie.Busy And ie.ReadyState = 4 Then
DoEvents
Application.Wait (Now + TimeValue("00:00:01"))
If Not ie.Busy And ie.ReadyState = 4 Then
Exit Do
End If
End If
DoEvents
Loop
End Sub
Sub TestSendMessageShort()
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
openpage "http://unixpapa.com/js/testkey.html"
waitForLoad 'function that checks if document loading is complete
Set varInput = ie.document.getElementById("t")
varInput.Value = "This is a text string"
varInput.Focus
'--------------- This piece of code doesn't work
SendMessage ie.hwnd, WM_KEYDOWN, VK_RETURN, 0
SendMessage ie.hwnd, WM_KEYUP, VK_RETURN, 0
'--------------- The Below piece of code works (intermittently) ----------
AppActivate ie.document.Title, True
Application.Wait (Now + TimeValue("00:00:01"))
SendKeys "{ENTER}", True
DoEvents
Application.Wait (Now + TimeValue("00:00:02"))
'ie.Quit
Set ie = Nothing End Sub