View Full Version : SendObject code


Digger442
02-08-2006, 06:41 AM
Hey folks,

Using the following code I am creating an e-mail (Outlook) from a form.

The issue I want to resolve is in the Subject line. As can be seen in the code, the Subject Line (code highlighted) of the e-mail will have "Patient Note:" in it. I would like to add the last name of the patient from a text box on the form.\

I cannot figure out the proper syntax to have both.

Help if you can!!

Thanks!!

Private Sub Command0_Click()
If IsNull(Me.Send_to_box) Then
MsgBox "There is no email address."
Else
DoCmd.SendObject , , "RichTextFormat(*.rtf)", Me.Send_to_box, Me.CC_box, , "Patient Note:", Contact_Note, True, ""
End If
Close

End Sub

sportsguy
02-08-2006, 11:11 AM
Copy this function into a public module.
Call this Function from your form after saving the file that you want to send.





'Uses Microsoft Outlook 9 Object library.
'stTo - recipient of email
'stCc - as above
'stBcc - as above
'stSubject - subject line of email
'stMsg - actual text of email - use " & vbcrlf & "... to specify a new line
'stAttachment - string value of the path to the attachments with ";" as a delimiter


Public Sub GenerateEmail(stTo, stSubject, stMsg, boPreview, stCc, stBcc, stAttachment)

'Error Handling
On Error GoTo Err_GenerateEmail
Dim obEmail As Object
Dim obMsg As Object
Dim obAttachments As Object
Dim stDelimiter As String
stDelimiter = ";"
Set obEmail = CreateObject("Outlook.Application")
Set obMsg = obEmail.CreateItem(olMailItem)
Set obAttachments = obMsg.Attachments

With obMsg
.Subject = stSubject 'set subject line
.To = stTo 'set SendTo email address
If Not stCc = "None" Then 'Check for email address for Cc
.CC = stCc 'If true then set Cc email address
End If
If Not stBcc = "None" Then 'Check for email address for Cc
.Bcc = stBcc 'If true then set Cc email address
End If
.Body = stMsg 'set body of msg
End With


If Not stAttachment = "None" Then
Dim vaAttachmentArray() As Variant
Dim inArraySize As Integer
Dim i As Integer
While InStr(stAttachment, stDelimiter) > 0
ReDim Preserve vaAttachmentArray(0 To inArraySize) As Variant
vaAttachmentArray(inArraySize) = Left(stAttachment, InStr(stAttachment, stDelimiter) - 1)
stAttachment = Mid(stAttachment, InStr(stAttachment, stDelimiter) + 1)
inArraySize = inArraySize + 1
Wend
ReDim Preserve vaAttachmentArray(0 To inArraySize) As Variant
vaAttachmentArray(inArraySize) = stAttachment
For i = 0 To UBound(vaAttachmentArray())
obAttachments.Add vaAttachmentArray(i)
Next
End If

With obMsg
If boPreview = True Then 'Check for preview flag
.Display 'if true, display
Else
.Send 'if false, then send email immediately
End If
End With

Set obEmail = Nothing 'Clear object variable
Set obMsg = Nothing 'Clear object variable
Set obAttachments = Nothing 'Clear object variable

Exit_Err_GenerateEmail: 'function call for exit error, if error occurs
Exit Sub 'Exit function
Err_GenerateEmail: 'function call for error handling
'inform user that an unknown error has occured and call exit error
'(use of the following should be done only if all errors that can occur have been handled by function)
MsgBox Err.Number & " " & Err.Description, vbCritical + vbOKOnly ' GetSummaryInfo("Company")
Resume Exit_Err_GenerateEmail
End Sub

Digger442
02-09-2006, 06:22 AM
Thanks for the reply!

This seems to be a bit more involved than I need.

Within the SendObject function (using the Event Procedure in my original post) is there anyway to have both pre-entered text and text box results in the Subject line of the created E-mail?

Thanks!!

Digger442
02-09-2006, 11:57 AM
bumping up

Jerry Stoner
02-09-2006, 12:59 PM
DoCmd.SendObject , , "RichTextFormat(*.rtf)", Me.Send_to_box, Me.CC_box, , "Patient Note:" & " " & Me.txtYourBox, Contact_Note,

Digger442
02-09-2006, 01:00 PM
Thanks much!! I'll give that a shot.