2013-12-09 64 views
1

我有用于发送自动电子邮件的私人子功能的代码。我从来源皮特靠在代码自动电子邮件功能

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 = "[email protected]" 
         .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 
+0

如果我删除IF声明它的作品,但是这是表单提交时需要。 Combo99(名称将很快更改)是我需要检查不再支持的模型的位置。 – ASM2701

回答

1

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

If Combo99.Text = "SM" Or "TW" Or "LM" Or "LV" Or "SV" Then 
  1. 的组合的Text属性仅当它具有焦点。当焦点转移到其他控件时,例如用户单击某个命令按钮时,请改用Combo99.Value

  2. 当您在条件之间使用Or时,您必须再次为每个Or重复=符号左侧的项目。

考虑这两个If语句...

If strLetter = "a" Or "b" Then 
If strLetter = "a" Or strLetter = "b" Then 

首先抛出一个错误,但第二次却没有。

如果您想比较某些值与值列表,可以使用Select Case

Select Case Me.Combo99.Value 
Case "SM", "TW", "LM", "LV", "SV" 
    ' insert code to send email 
Case Else 
    ' no email 
End Select 
+0

感谢您的快速回复Hans ...当我尝试运行代码时,出现了编译错误,其中使用ME无效。 – ASM2701

+0

那么如果你放弃“我”会发生什么? – HansUp

+0

获取以下错误。 编译错误: 变量没有定义 – ASM2701