Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
(ByVal hwnd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDlgCtrlID Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CheckRadioButton Lib "user32" (ByVal hDlg As Long, ByVal nIDFirstButton As Long, _
ByVal nIDLastButton As Long, ByVal nIDCheckButton As Long) As Long
Private Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Const GW_HWNDNEXT = 2
Private Const GW_OWNER = 4
Private Const BM_CLICK = &HF5
Private Const WM_CLOSE = &H10
Private Const WM_COMMAND = &H111
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SETTEXT As Long = &HC
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONDBLCLK As Long = &H203
Private Const WM_PASTE As Long = &H302
Private Const WM_COPY As Long = &H301
Private Const WM_COPYDATA As Long = &H4A
Private Const WM_CUT As Long = &H300
Private Const CB_FINDSTRINGEXACT = &H158
Private Const CB_ERR = (-1)
Private Const CB_FINDSTRING = &H14C
Private Const CB_GETCOUNT = &H146
Private Const CB_GETCURSEL = &H147
Private Const CB_GETTOPINDEX = &H15B
Private Const CB_SETCURSEL = &H14E
Private Const CB_SHOWDROPDOWN = &H14F
Private Const VK_DOWN = &H28
Private Const VK_UP = &H26
Private Const VK_SELECT = &H29
Private Const VK_SPACE = &H20
Private Const VK_TAB = &H9
Private mlHwnd As Long
Private mlHwndSel As Long
Private mlHwndFields As Long
Private mlHwndExistDoc As Long
Private mlHwndExistDocPath As Long
Private mlHwndDocType As Long
Private mlHwndMergeTo As Long
Private mlHwndBrowse1 As Long
Private mlHwndCD As Long
Private mlHwndCDPath As Long
Private mlHwndCDOK As Long
Private mlHwndOK As Long
Public Sub Main()
Dim rtn As Long
Dim arSwitches() As String
Dim i As Integer
Do While FindWindow("#32770", "Mail Merge Contacts") = 0
DoEvents
Loop
mlHwnd = FindWindow("#32770", "Mail Merge Contacts")
arSwitches = Split(Command, "/")
If UBound(arSwitches) = 0 Then
MsgBox "Missing startup parameter!", vbOKOnly + vbCritical
End
End If
'SELECT ONLY SELECTED CONTACTS
mlHwndSel = FindWindowEx(mlHwnd, 0&, "Button", "&Only selected contacts")
rtn = SendMessage(mlHwndSel, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndSel, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
'SELECT ALL CONTACT FIELDS
mlHwndFields = FindWindowEx(mlHwnd, 0&, "Button", "All contact &fields")
rtn = SendMessage(mlHwndFields, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndFields, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
'SELECT PATH TO NEW/EXISTING DOCUMENT
If arSwitches(1) = "New" Then
mlHwndExistDoc = FindWindowEx(mlHwnd, 0&, "Button", "&New document")
Else
mlHwndExistDoc = FindWindowEx(mlHwnd, 0&, "Button", "&Existing document:")
End If
rtn = SendMessage(mlHwndExistDoc, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndExistDoc, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
'SELECT THE DOCUMENT TYPE
mlHwndDocType = FindWindowEx(mlHwnd, 0&, "REComboBox20W", vbNullString)
rtn = SendMessage(mlHwndDocType, CB_SHOWDROPDOWN, ByVal 1, ByVal 0& ) 'DROPDOWN
If rtn = 1 Then
'ALREADY SELECTED BY DEFAULT. IF NOT CHANGE SO ITS SYNCHED
'FORM LETTERS
rtn = SendMessage(mlHwndDocType, CB_SETCURSEL, 0&, 0& ) 'SET TO FIRST OPTION
'MAILING LABELS
'' rtn = SendMessage(mlHwndDocType, WM_KEYDOWN, VK_DOWN, 0& ) 'MOVE DOWN 1
'' rtn = SendMessage(mlHwndDocType, WM_KEYUP, VK_DOWN, 0& )
'ENVELOPES
'' rtn = SendMessage(mlHwndDocType, WM_KEYDOWN, VK_DOWN, 0& ) 'MOVE DOWN 1
'' rtn = SendMessage(mlHwndDocType, WM_KEYUP, VK_DOWN, 0& )
'CATALOGS
'' rtn = SendMessage(mlHwndDocType, WM_KEYDOWN, VK_DOWN, 0& ) 'MOVE DOWN 1
'' rtn = SendMessage(mlHwndDocType, WM_KEYUP, VK_DOWN, 0& )
'MAKE THE SELECTION
rtn = SendMessage(mlHwndDocType, CB_SETCURSEL, ByVal 0&, ByVal 0& ) 'MAKE SELECTION
rtn = SendMessage(mlHwndDocType, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndDocType, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndDocType, CB_SHOWDROPDOWN, ByVal 0, ByVal 0& ) 'CLOSE UP
End If
'MERGE TO:
mlHwndMergeTo = GetWindow(mlHwndDocType, GW_HWNDNEXT)
mlHwndMergeTo = GetWindow(mlHwndMergeTo, GW_HWNDNEXT)
rtn = SendMessage(mlHwndMergeTo, CB_SHOWDROPDOWN, ByVal 1, ByVal 0& ) 'DROPDOWN
If rtn = 1 Then
'NEW DOCUMENT
rtn = SendMessage(mlHwndMergeTo, CB_SETCURSEL, 0&, 0& ) 'SET TO FIRST OPTION
'PRINTER
' rtn = SendMessage(mlHwndMergeTo, WM_KEYDOWN, VK_DOWN, 0& ) 'MOVE DOWN 1
' rtn = SendMessage(mlHwndMergeTo, WM_KEYUP, VK_DOWN, 0& )
'EMAIL ADDRESS
' rtn = SendMessage(mlHwndMergeTo, WM_KEYDOWN, VK_DOWN, 0& ) 'MOVE DOWN 1
' rtn = SendMessage(mlHwndMergeTo, WM_KEYUP, VK_DOWN, 0& )
'MAKE THE SELECTION
rtn = SendMessage(mlHwndMergeTo, CB_SETCURSEL, ByVal 0&, ByVal 0& ) 'MAKE SELECTION - MAILING LABELS
rtn = SendMessage(mlHwndMergeTo, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndMergeTo, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndMergeTo, CB_SHOWDROPDOWN, ByVal 0, ByVal 0& ) 'CLOSE UP
End If
'ENTER THE PATH IF PARAMETER IS A EXISTING DOCUMENT TYPE
If arSwitches(1) <> "New" Then
'NEED TO PASTE THE FILE APTH IN SO IT TRIGGERS THE VERIFICATION OF DOCUMENT BY OUTLOOK
Clipboard.Clear
Clipboard.SetText arSwitches(1) 'FILE PATH AND NAME
mlHwndExistDocPath = FindWindowEx(mlHwnd, 0&, "RichEdit20WPT", vbNullString)
' rtn = SendMessage(mlHwndExistDocPath, WM_SETTEXT, 0&, ByVal "D:\My Documents\Doc1.doc")
rtn = SendMessage(mlHwndExistDocPath, WM_LBUTTONDOWN, ByVal 0&, ByVal 0& )
rtn = SendMessage(mlHwndExistDocPath, WM_LBUTTONUP, ByVal 0&, ByVal 0& )
rtn = PutFocus(mlHwndExistDocPath)
rtn = SendMessage(mlHwndExistDocPath, WM_PASTE, ByVal 0&, ByVal 0& )
rtn = PutFocus(mlHwndExistDocPath)
rtn = SendMessage(mlHwndExistDocPath, WM_LBUTTONDBLCLK, ByVal 0&, ByVal 0& )
End If
'CLICK OK
mlHwndOK = FindWindowEx(mlHwnd, 0&, "Button", "OK")
PostMessage mlHwndOK, BM_CLICK, 0&, 0&
End Sub