2013-12-09 59 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