Send an Outlook Express attachment

Started by nandagopal, Nov 04, 2008, 07:37 PM

Previous topic - Next topic

nandagopal

This program requires Outlook Express to be your default e-mail program.

The program uses ShellExecute to start sending an email with Outlook, filling in the message's To, Subject, and Body fields.

It then uses FindWindow to find a window with title equal to the mail message's Subject (that's the title Outlook Express gives the new mail window). It loops until it finds this window.

Next the program uses SendKeys to send the new window the keys Alt-I-A. That invokes the Isert menu's File Attachment command. The program adds the zip file's name, sends two tabs to move to the Attach button, and sends Enter to trigger the button.


Private Sub Command1_Click()
Dim zip_file As String, Ret As Long

    ' Start Outlook Express email.
    zip_file = App.Path & "\test.zip"
    ShellExecute Me.hWnd, "Open", _
        "mailto:Anyone@anywhere.com?subject=" & zip_file & _
        "&body=Zip File Attached", vbNullString, _
        vbNullString, vbNormalFocus

    ' Wait until Outlook Express is ready.
    While Ret = 0
        DoEvents
        Ret = FindWindow(vbNullString, zip_file)
    Wend

    ' Send keys Alt-I-A, the zip file name,
    ' two TABs, and Enter.
    SendKeys "%ia" & zip_file & "{TAB}{TAB}{ENTER}"
End Sub