Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email from Access

Status
Not open for further replies.

jmthompson

Technical User
Oct 21, 2002
6
US
Hello All!

I am sitting her banging my head against this computer, I have read some of the questions about email and access and all have been very helpful and everything has worked. The problem that I am having is that I want my email to to be populated by the info in the form such as the email address cust name and etc... How can this be done?

Thanks
 
Here's a procedure I just created for myself today (got most of the code from a Microsoft site). Call the following routine and pass your arguments from the form, something like this:

Call SendEmailMessage(Me!txtSubject, Me!txtBody, Me!txtRecipient...)

Code:
Sub SendEmailMessage(strSubject As String, _
                    strBody As String, _
                    strRecipient As String, _
           Optional strCC As String, _
           Optional strBCC As String, _
           Optional bolDisplayMsg As Boolean = False, _
           Optional strAttachmentPath As String)
    
'********************************
'*  Declaration Specifications  *
'********************************

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment

    On Error GoTo ErrHandler

'*********************************
'*  Create the Outlook session.  *
'*********************************

    Set objOutlook = CreateObject("Outlook.Application")

'*************************
'*  Create the message.  *
'*************************

    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
        
'*********************************************
'*  Add the To recipient(s) to the message.  *
'*********************************************

        Set objOutlookRecip = .Recipients.Add(strRecipient)
        objOutlookRecip.Type = olTo

'*********************************************
'*  Add the CC recipient(s) to the message.  *
'*********************************************
        
        If (strCC <> vbNullString) Then
            Set objOutlookRecip = .Recipients.Add(strCC)
            objOutlookRecip.Type = olCC
        End If
        
'**********************************************
'*  Add the BCC recipient(s) to the message.  *
'**********************************************

        If (strBCC <> vbNullString) Then
           Set objOutlookRecip = .Recipients.Add(strBCC)
           objOutlookRecip.Type = olBCC
        End If
        
'***********************************************************
'*  Set the Subject, Body, and Importance of the message.  *
'***********************************************************

       .Subject = strSubject
       .Body = strBody
       .Importance = olImportanceHigh  'High importance

'*************************************
'*  Add attachments to the message.  *
'*************************************
       
       If (strAttachmentPath <> vbNullString) Then
           Set objOutlookAttach = .Attachments.Add(strAttachmentPath)
       End If

'************************************
'*  Resolve each Recipient's name.  *
'************************************

       For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
       Next

'***************************************************
'*  Should we display the message before sending?  *
'***************************************************

       If bolDisplayMsg Then
           .Display
       Else
           .Save
           .Send
       End If
    
    End With
    
    Set objOutlook = Nothing

'********************
'*  Exit Procedure  *
'********************
        
ExitProcedure:

    Exit Sub

'****************************
'*  Error Recovery Section  *
'****************************
        
ErrHandler:
    
    Msgbox Err.Number & vbcrlf & Err.Description

    Resume ExitProcedure

End Sub
 
By the way, you will need to set a Reference to &quot;Microsoft Outlook 9.0 Object Library&quot;. You might also want to have the routine return an error code so that you will know whether or not (programmatically) there was an error trying to send the email.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top