PDA

View Full Version : Solved: Send An Email



Dowsey1977
10-11-2005, 04:47 AM
Hi,

I am using the below code to send an email with pre-populated recipients, subject and password protection. I want to add in the body text as well, but can't figure out a good way of doing it. The SendMail desn't allow for this, and I've tried all sorts of stuff with CreateObject, but can't figure all this out.


Private Sub CommandButton1_Click()
Dim Rcpts(1 To 3) As String
Dim Name, Password As String
Application.ScreenUpdating = False
If txtPassword.Value = "" Then
MsgBox "A password must be entered for Form 6 to be sent", vbCritical, "Password Entry Error"
Else
Unload Me 'Gers rid of the userform
Name = "Form 6 Cash Processing Request - " & Range("D97").Value 'Subject Of email (Range 97 Value is transaction type)
Password = txtPassword.Value 'This is the textbox on the userform
ThisWorkbook.SaveAs FileName:=Name, Password:=Password 'Saves the workbook with Name and Password
Rcpts(1) = "simon.dowse@Domain.com" 'Recipient email adress
Rcpts(2) = ""
Rcpts(3) = ""
ThisWorkbook.SendMail Recipients:=Rcpts, Subject:=Name, ReturnReceipt:=True 'Details on email
MsgBox "The report has now been emailed to Cash Processing for action " & Application.UserName 'Messagebox confirming send
End If
Application.ScreenUpdating = True
End Sub

Any ideas??

:motz2: :bow:

gibbo1715
10-11-2005, 04:53 AM
Have a look at the attached, may give you some ideas for an alternative approach

Gibbo

http://vbaexpress.com/kb/getarticle.php?kb_id=770

gibbo1715
10-11-2005, 05:11 AM
Heres a cutdown version, Remember to set a reference to outlook

Gibbo

Option Explicit
'Set a reference to OUTLOOK
Sub Email()
Dim sTo As String
Dim sName As String
Dim sBody As String
Dim iImportance As Integer
Dim bReceipt As Boolean
Dim bSucces As Boolean
Dim Rcpts() As String
Dim Name, Password As String
Application.ScreenUpdating = False
If txtPassword.Value = "" Then
MsgBox "A password must be entered for Form 6 to be sent", vbCritical, "Password Entry Error"

Else
' Unload Me 'Gers rid of the userform
Name = "Form 6 Cash Processing Request - " & Range("D97").Value 'Subject Of email (Range 97 Value is transaction type)
Password = txtPassword.Value 'This is the textbox on the userform
ThisWorkbook.SaveAs Filename:=Name, Password:=Password 'Saves the workbook with Name and Password

'Error Handling
On Error GoTo Err_Mail

sTo = "simon.dowse@Domain.com" & ";someone.else@Domain.com" 'Recipient email adress
sName = Name
sBody = "Body"

bSucces = CreateMailItem(sTo, sName, sBody, 2, True)
MsgBox "The report has now been emailed to Cash Processing for action " & Application.UserName 'Messagebox confirming send
'End If
Application.ScreenUpdating = True


Err_Mail:

End Sub

Function CreateMailItem(sTo As String, sName As String, sBody As String, _
iImportance As Integer, bReceipt As Boolean) As Boolean

Dim oOutlookApp As Object
Dim oOutlookMail As Object

CreateMailItem = False
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
End If
If Not oOutlookApp Is Nothing Then
Set oOutlookMail = oOutlookApp.CreateItem(0)
If Not oOutlookMail Is Nothing Then
With oOutlookMail
.To = sTo
.Subject = sName
.Body = sBody
.Importance = iImportance
.ReadReceiptRequested = bReceipt
.Display
CreateMailItem = True
End With
End If
End If

Set oOutlookMail = Nothing
Set oOutlookApp = Nothing
End Function

Steiner
10-13-2005, 05:23 AM
And here's an example without using Outlook but the MailAPI, therefore you don't need an Outlook-reference, it will use the program that Windows recognizes as it's standard mail client:
http://vbaexpress.com/kb/getarticle.php?kb_id=311

Daniel