To open a default client, you can use the ShellExecute funciton which recognises email addresses if they are prefixed with mailto: and run the default program.
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(sDest As String, _
Optional sSubject As String, _
Optional sBody As String, _
Optional sCC As String, _
Optional sBCC As String)
Dim res As Long
res = ShellExecute 0, vbNullString, _
"mailto:" & sDest & _
"?subject=" & sSubject & _
"&body=" & sBody & _
"&CC=" & sCC & "&BCC=" & _
sBCC, 0&, 0&, 1
OpenEmailProgram = (res > 32)
End Function
NB!! Before to check the return result in case no e-mail program is 'defaulted'
If Not OpenEmailProgram("user@domain.com", _
"SendMail Test", "The function works!!!", _
"annother@domain.com, phathi@domain.com" Then
MsgBox "Unable to run the email program"
End If
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
Nowe that I've told you what you don't want to hear, here's something a bit closer to your reality.
Sub SendEmail(From As String, SendTo As String, Subject As String, _
EmailText As String, Optional AttachmentPath As String, _
Optional Attachment As String, Optional CC As String)
Const constRoutine As String = "SendEmail"
Dim strSendTo As String
Dim objSendMail As CDONTS.NewMail
Dim i As Integer
On Error GoTo TryMAPI
If SendTo = "" Then Exit Sub
Set objSendMail = New CDONTS.NewMail
With objSendMail
On Error Resume Next
.From = From
If CC <> "" Then
.CC = CC
End If
If AttachmentPath <> "" Then
If Right$(AttachmentPath, 1) <> "\" Then
AttachmentPath = AttachmentPath & "\"
End If
.AttachFile (AttachmentPath & Attachment)
End If
.Send
End With
Exit Sub
TryMAPI:
On Error GoTo ErrorHandler
'If CDO fails, try MAPI
If CC <> "" Then
strSendTo = SendTo & "; " & CC
Else
strSendTo = SendTo
End If
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.