1

I am trying to set the text value of an 'edit' control text box with the SendMessage() function. I am able to set the text about 75% of the time but sometimes the function fails. Why would it work sometimes and fail others? I am always getting the correct window handle and am confused as to what else it could be? Here's my code:

attempt = 1
retry:
    'Find open window textbox
    temp_id = 0
    temp_id = FindWindowEx(open_id, ByVal 0&, "ComboBoxEx32", vbNullString)
    temp_id = FindWindowEx(temp_id, ByVal 0&, "ComboBox", vbNullString)
    text_id = FindWindowEx(temp_id, ByVal 0&, "Edit", vbNullString)

    'Select textbox and enter text then press open button
    Call SendMessage(text_id, WM_LBUTTONDBLCLK, 0, 0)
    Sleep 100
    text = filename

    Call SendMessage(text_id, WM_SETTEXT, 0&, ByVal text)
    DoEvents
    Sleep 100

    If GetWindowTextLength(text_id) = 0 Then
        If attempt < 3 Then
            attempt = attempt + 1
            GoTo retry
        End If
    End If
Matt26
  • 61
  • 4
  • 1
    Rather than `Call SendMessage(...` assign the return value and look at the reason for the failure. `lngResult = SendMessage(...`. MSDN documentation, https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx – jac Aug 06 '15 at 21:56
  • Should it matter if the open dialogue box window is the 'ForgroundWindow'? I seem to notice it only works when the open dialogue box has the focus. – Matt26 Aug 07 '15 at 12:25
  • I believe it should, although I haven't tried sending a message to a box that doesn't have the focus before. When the open dialog box doesn't have the focus, I suspect that the child windows can't accept messages. If you follow jac's advice, I'll be interested to find what error turns up. – BobRodes Aug 10 '15 at 01:51
  • I got a value of 1 both when the text was set and when it wasn't. I believe the issue was that the open dialogue box either wasn't fully loaded or that the text box did not have the focus. I added a longer delay after opening the dialogue box and made sure to set the focus to the textbox before sending the message and this seemed to do the trick. This still seems fairly unreliable to me though.. Is there any way to wait until the new open dialogue window is fully loaded? Or do I just have to wait for an arbitrary amount of time and hope? – Matt26 Aug 10 '15 at 12:08
  • Yeah, I wouldn't much like that solution either. You might want to look into CBT hooking. In particular, look at SetWindowsHookEx and the CBTProc callback procedure. [Here's](http://www.tek-tips.com/viewthread.cfm?qid=1407322) a long thread where I learned how to alter fonts in a MsgBox dialog using it. – BobRodes Aug 17 '15 at 02:05

0 Answers0