'in a module
Option Explicit
'the last param was changed from "lParam As Any" to "ByVal lParam As Long", and StrPtr() will be used to give a pointer
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const LB_ADDSTRING As Long = &H180
Public Const LB_RESETCONTENT As Long = &H184
Public Const LB_SETSEL As Long = &H185
Public Const LB_GETTEXT As Long = &H189
Public Const LB_GETTEXTLEN As Long = &H18A
Public Const LB_GETCOUNT As Long = &H18B
Public Const GWL_STYLE As Long = -16
Public Sub CopyListBox(ByVal hWndSrcLB As Long, ByVal hWndDestLB As Long)
Dim lngItems As Long, lngLength As Long
Dim lngStyle As Long, i As Long
Dim strBuffer As String
'get total items in source listbox
lngItems = SendMessage(hWndSrcLB, LB_GETCOUNT, 0, 0)
'terminate sub if there's nothing to copy
If lngItems = 0 Then Exit Sub
'get the style of the source listbox
lngStyle = GetWindowLong(hWndSrcLB, GWL_STYLE)
'apply that style to the destination listbox
SetWindowLong hWndDestLB, GWL_STYLE, lngStyle
'clear the destination listbox
SendMessage hWndDestLB, LB_RESETCONTENT, 0, 0
'go through each item
For i = 0 To (lngItems - 1)
'note that although i is a long, a listbox will contain at max 32767 items (although memory constraints will reduce that based on the size of the items)
'get length of text of item i + 1 for null terminator
lngLength = SendMessage(hWndSrcLB, LB_GETTEXTLEN, i, 0) + 1
'create buffer
strBuffer = Space$(lngLength)
'get text of item i
lngLength = SendMessage(hWndSrcLB, LB_GETTEXT, i, StrPtr(strBuffer))
'remove null terminator
strBuffer = Left$(strBuffer, lngLength)
'place text from source into destination
SendMessage hWndDestLB, LB_ADDSTRING, 0, StrPtr(strBuffer)
Next i
End Sub