Ok…I’ve been trying to set up a function in visual Basic that will automatically send an email without actually opening Outlook.
Right now, I can get Visual Basic to open up Outlook, with the header and message body filled in, but I would like to be able to have the email sent without forcing the user to click on “send” within Outlook.
Here is the code that I have:
Within a module:
Private Declare Function ShellExecute Lib “shell32.dll” Alias “ShellExecuteA” _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
’ Open the default program for sending email messages
’ Returns True if successful, False otherwise
Public Function OpenEmailProgram(ByVal EmailAddress As String) As Boolean
Dim res As Long
res = ShellExecute(0&, “open”, “mailto:” & EmailAddress, vbNullString, _
vbNullString, vbNormalFocus)
OpenEmailProgram = (res > 32)
End Function
'SendMail does almost the same thing as OpenEmailProgram,
'except that it allows a message to be prewritten
Public Sub SendMail(sDest As String, Optional sSubject As String, _
Optional sBody As String, Optional sCC As String)
ShellExecute 0, vbNullString, “mailto:” & sDest & “?subject=” & sSubject & _
“&body=” & sBody & “&CC=” & sCC, 0&, 0&, 1
End Sub
Then, in a form’s sub:
SendMail “darmbrust@setfocus.com”, “SendMail Test”, “The function works!!!”
If anyone has any suggestions on what I could do differently, so that the user does not have to interact with outlook, I would appreciate it.