Combobox: WM_SETTEXT doesn't work while Sendkeys works ???



BACKGROUND:

I need to transfer data from an excel workbook to an *.exe program. This is
further to my last post.

I have successfully drilled down to each control window in the *.exe using
FindWindowEx. I pass the desired window handles to an array for later
processing. I then simulate clicking a command button in the *.exe to allow
adding a new record. I then loop through the array extracting each window
handle and also loop through source cells in the excel wb. Cell values are
then exported to the corresponding control windows in the *.exe.

I have found that the only way I can get the values to "take" is to first
set focus to the windows (WM_SETFOCUS), then set the text (WM_SETTEXT), then
kill the focus (WM_KILLFOCUS). If I don't do this then the *.exe acts like
the values aren't there and screws up the calculations and clears the
windows. The set focus / kill focus method works fine except for comboboxes.

PROBLEM:

As mentioned, the above method works except for comboboxes. The values paste
but the *.exe acts blind to them and they don't get added to the *.exe's
database when the next record is added during the loop. Evidently, a
requisite event doesn't fire.

Of note, an earlier code incarnation using SendKeys worked except slow and
risky. So the event must fire when values are passed to windows through the
message queue but not to window procedures directly. This I don't understand.

I've tried using SendMessage and WM_COMMAND referencing the control ID's.
I've tried pretty much every permutation possible. In the code example, I use
the MakeLong function derived from a post by Karl E. Peterson. Doesn't help
(but maybe I’m doing it wrong).

REQUEST:

Hoping someone can review the code and suggest what the problem may be plus
suggest possible solutions. This project is both for practical need and my
education. Insights into what's going on much appreciated.

CURRENT CODE VERSION:

Const CBN_EDITCHANGE = &H6
Const WM_SETTEXT = &HC
Const WM_SETTEXT = &HC
Const WM_LButtonUp = &H202
Const WM_LButtonDown = &H203
Const WM_SETFOCUS = &H7
Const WM_KILLFOCUS = &H8
Const WM_COMMAND = &H111

Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hWnd As Long) As Long
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 GetParent Lib "user32" (ByVal hWnd As Long) As Long

Sub ExportMain(r As Range)
Dim c As Range
Dim id As Long, wp As Long
Dim txt As String
Dim hWndArr(0 To 10) As Long
Dim OffsetArr As Variant

'Excluded is code that drills down to each control window using
'FindWindowEx. The desired control window handles are added to the
'hWndArr array. The first two handles represent comboboxes. The
'rest represent Edit controls except for the last handle which
'represents a command button - i.e. hWndArr(10).

'OffsetArr is an array of Excel column numbers used to specify
'cell addresses. Values in these cells are exported to the windows
'specified by handles contained in the hWndArr array.

OffsetArr = Array(11, 0, 1, 2, 6, 7, 3, 5, 8, 10)
'Loop through each cell in column B containing test numbers...
For Each c In r.Cells
If Len(c.Value) > 0 And IsNumeric(c.Value) Then
'Simulate clicking command button to allow adding another
'record. This works.
SendMessage hWndArr(10), WM_SETFOCUS, ByVal 0&, ByVal 0&
SendMessage hWndArr(10), WM_LButtonDown, ByVal 0&, ByVal 0&
SendMessage hWndArr(10), WM_LButtonUp, ByVal 0&, ByVal 0&
SendMessage hWndArr(10), WM_KILLFOCUS, ByVal 0&, ByVal 0&
'Loop through hWndArr extracting window handles. Also loop
'through Excel cells in same row as c extracting values to
'export to the windows. This works fine.
For x = 0 To 8
txt = c(1, OffsetArr(x)).Value
Select Case x
Case 0, 1 'If handle refers to combobox then...
id = GetDlgCtrlID(hWndArr(x))
wp = MakeLong(CBN_EDITCHANGE, id)
Case 5
'translate for unit difference
txt = txt / 1000
End Select
'This works except for comboboxes. Values pasted with WM_SETTEXT
'don't take - i.e. don't get added to the application's data base.
'When records are recalled, combobox values are blank while those
'added to Edit controls are fine.
SendMessage hWndArr(x), WM_SETFOCUS, ByVal 0&, ByVal 0&
SendMessage hWndArr(x), WM_SETTEXT, ByVal 0&, ByVal txt
'If array element 0 or 1 (combobox handles) then...
If x < 2 Then SendMessage GetParent(hWndArr(x)), WM_COMMAND, wp,
ByVal 0&
SendMessage hWndArr(x), WM_KILLFOCUS, ByVal 0&, ByVal 0&
Next
End If
Next

End Sub

'Function obtained from Karl E. Peterson post
Function MakeLong(ByVal WordHi As Long, WordLo As Long) As Long
MakeLong = (CLng(WordHi) * &H10000) Or (WordLo And &HFFFF)
End Function

Many thanks,
Greg
.