PDA

Click to See Complete Forum and Search --> : send email body


BazP
Jul 26th, 2005, 07:10 AM
I would like to send an attachment to a user and include some (body) text to it.

I'm putting it into the subject, but its getting too long.

Code im using is...

application.dialogues(xldialogsendmail).show user, subject

Can anyone help?

dannymking
Jul 27th, 2005, 03:45 AM
For this I would try and use Microsoft Outlook directly in the code rather than the sendmail..

Irrespective of the Application you are using this can be accomplished by adding the Microsoft Outlook reference into the Tools, References in the Visual Basic Editor (ALT+F11) just scroll down until you find it.


Function WriteEmail(pTo As String, pSubject As String, pBody As String, pIsAttach As Boolean, _
Optional pCC As String = "", Optional pBCC As String = "", Optional pAttachLoc As String = "") As Boolean
Dim obOT As Outlook.Application
Dim obMI As Outlook.MailItem
Dim obAT As Outlook.Attachment
WriteEmail = False 'set the function to fail initially
'check for attachment first
If pIsAttach And pAttachLoc = "" Then
MsgBox "You have requested an attachment but have not supplied a filename", vbCritical, "No Attachement"
GoTo ExitProc
End If
'check the remaining required fields, in not populated then error out
If pTo = "" Then
MsgBox "Please supply at least one address to send to.", vbCritical, "No To Address"
GoTo ExitProc
End If
If pSubject = "" Then
MsgBox "Please supply a subject for this email.", vbCritical, "No Subject"
GoTo ExitProc
End If
If pBody = "" Then
MsgBox "Please supply a body of this email.", vbCritical, "No Body"
GoTo ExitProc
End If

'Get Outlook if it exists, if not attempt to create an instance
On Error GoTo CreateInst
Set obOT = GetObject(, "Outlook.Application")
GoTo WriteMail

CreateInst:
On Error GoTo NoOutlook
'Create the instance of outlook, if this fails at this point either there is not outlook installed
'or there is an application error.
Set obOT = CreateObject("Outlook.Application")
GoTo WriteMail

WriteMail:
On Error GoTo NoOutlook
Set obMI = obOT.CreateItem(olMailItem)
obMI.To = pTo
If pCC <> "" Then obMI.CC = pCC
If pBCC <> "" Then obMI.BCC = pBCC
If pIsAttach Then
Set obAT = obMI.Attachments.Add(pAttachLoc)
End If
obMI.Subject = pSubject
obMI.Body = pBody
obMI.Send
WriteEmail = True

ExitProc:
'close down the outlook object
On Error Resume Next
Set obAT = Nothing
Set obMI = Nothing
Set obOT = Nothing
On Error GoTo 0
Exit Function

NoOutlook:
MsgBox Err.Description, vbCritical, Err.Number
GoTo ExitProc
End Function

I use this myself in an access database..

You will however get a lot of confirmation notices due to outlook security, but it is pretty robust as it stands..

to use it do something like


If Not WriteEmail("PersonTo","subject","Body",True, , , "Filename") Then
Msgbox "Email Failed"
End If