-->

ACCESS 2007 - Automatically Send and Email Using O

2019-09-02 23:43发布

问题:

I am trying to create an App in Microsoft Access 2007. How can I silently send an email out using Outlook 2007 upon a specific event without any user interaction. How should I approach this. If you can provide some VBA some it would be extremely nice, but if not, could you guide me in how to accomplish this?

回答1:

I was able to solve my problem with the following code:

Public Sub SendEmail()

    Email_Bcc = "email@domain.com"
    Email_Body = "Email body!!!!"
    Email_Subject = "Email Subject"

    On Error GoTo debugs

    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)

    With Mail_Single
        .Subject = Email_Subject
        .To = Email_Send_To
        .cc = Email_Cc
        .BCC = Email_Bcc
        .Body = Email_Body
        .send
    End With

debugs:
    If Err.Description <> "" Then MsgBox Err.Description

End Sub


回答2:

First declare a couple variables in the event that you want to send the email, or in a function you'd like the event to call.

Public Started As Boolean
Public oApp As Outlook.Application
Public oItem As Outlook.MailItem

Next, open or get Outlook if it's running.

On Error Resume Next
'Get Outlook if it's running
Set oApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
     'Outlook wasn't running, start it from code
     Set oApp = CreateObject("Outlook.Application")
    Started = True
End If

Now, do what you've got to do with your email.

 Set oItem = oApp.CreateItem(olMailItem)
 With oItem
    .To = "email@email.com"
    .Subject = "Your email, sirrah."
    .Body = "Please enjoy this complimentary email."
    'Send the email
    .Send
End With

Finally, close outlook if it wasn't running before and clean up.

Set oItem = Nothing
If Started Then
    oApp.Quit
End If


回答3:

You can do this "code-free" by using a macro and the "SendObject" action. Be sure to complete all the necessary arguments To, Subject, Message Text, and then set the Edit Message argument to 'No'.

You can then attach this macro to any event you wish, such as the OnClick event of a button.