Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _ hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _ As Long Const CB_RESETCONTENT = &H14B Const CB_GETCOUNT = &H146 Const CB_GETITEMDATA = &H150 Const CB_SETITEMDATA = &H151 Const CB_GETLBTEXT = &H148 Const CB_ADDSTRING = &H143
Sub DuplicateComboBox(Source As ComboBox, Target As ComboBox, _ Optional AppendMode As Boolean) Dim index As Long Dim itmData As Long Dim numItems As Long Dim sItemText As String sItemText = Space$(512) LockWindowUpdate Target.hWnd If Not AppendMode Then SendMessage Target.hWnd, CB_RESETCONTENT, 0, ByVal 0& End If numItems = SendMessage(Source.hWnd, CB_GETCOUNT, 0&, ByVal 0&) For index = 0 To numItems - 1 SendMessage Source.hWnd, CB_GETLBTEXT, index, ByVal sItemText itmData = SendMessage(Source.hWnd, CB_GETITEMDATA, index, ByVal 0&) SendMessage Target.hWnd, CB_ADDSTRING, 0&, ByVal sItemText SendMessage Target.hWnd, CB_SETITEMDATA, index, ByVal itmData Next LockWindowUpdate 0 End Sub |