Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sendmessage win api function not working with IE8

Status
Not open for further replies.

vbatinker

Programmer
May 26, 2010
2
US
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 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_SETTEXT = &HC

Private Const WM_USER As Long = &H400


Sub TestSendMessageShort()


Set ie = CreateObject("internetexplorer.application")
ie.Visible = True

openpage " waitForLoad
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
 


Hi,

What APPLICATION are you coding in?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

I am programming in VBA as part of Microsoft Excel 2007.

Sorry for leaving that detail out...I thought I caught everything. Let me know if I am missing anything else. Thanks again for your prompt response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top