自动电子邮件功能

时间:2013-12-09 13:39:59

标签: ms-access ms-access-2007 access-vba outlook-2007

我有一个用于发送自动电子邮件的私有子功能的代码。我从Pete Leaning那里采购了代码

ACCESS 2007 - Automatically Send and Email Using Outlook Upon a Specific Event

我尝试使用下面的代码将其变成一个函数。但它不起作用。我有一种感觉,我完全错了。另外,如果可能的话,我希望电子邮件的正文包含整个记录信息。

Option Explicit
Public Started As Boolean
Public oApp As Outlook.Application
Public oItem As Outlook.MailItem
Function AutoEmail()



'Automatic Email to send notifications to selected user
If Combo99.Text = "SM" Or "TW" Or "LM" Or "LV" Or "SV" Then


    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

                     Set oItem = oApp.CreateItem(olMailItem)
                     With oItem
                        .To = "google@google.com"
                        .Subject = "AutoEmail Test"
                        .Body = "Please enjoy this complimentary email. If this worked please email back."
                        'Send the email
                        .Send
                    End With

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

'Display message to the user
MsgBox "A model that is on the watch list has been selected. An Automatic Email has been sent", vbOKOnly, AutoEmail

Else
        'Do nothing
End If


End Function

1 个答案:

答案 0 :(得分:1)

此代码行有两个问题......

If Combo99.Text = "SM" Or "TW" Or "LM" Or "LV" Or "SV" Then
  1. 组合的Text属性仅在具有焦点时才可用。当焦点转移到不同的控件时,例如当用户单击命令按钮时,请改为使用Combo99.Value

  2. 在条件之间使用Or时,您必须再次为=的{​​{1}}符号左侧重复该项目。

  3. 考虑这两个Or陈述......

    If

    第一个抛出错误,但第二个没有。

    如果您希望将某些内容与值列表进行比较,则可以使用If strLetter = "a" Or "b" Then If strLetter = "a" Or strLetter = "b" Then

    Select Case