0

到目前为止,我有第一个推荐人打开 Outlook 并使用自动自定义消息检索正确的电子邮件地址。现在,我正在尝试添加第二个推荐,Referral_To_ID_2作为可选,这意味着如果没有选择它就不会引发错误。下面是我到目前为止的表单和代码,但我不太确定我是否通过分配对我的第二个对象的引用Referral_To_ID_2以及If语句来做对了。现在,如果我没有选择任何东西,它实际上给了我一个错误Referral_To_ID_2谁能解释我添加第二个对象作为可选对象的最佳方法?非常感谢!

在此处输入图像描述

Private Sub cmdReferralEmail_Click()

Dim emailAddresses As Collection, newEmail As Email
Dim emailAddresses2 As Collection, newEmail2 As Email

Set emailAddresses = GetEmails(referralId:=Referral_To_ID)
Set emailAddresses2 = GetEmails(referralId:=Referral_To_ID_2)

If emailAddresses Is Nothing Then 'User cancelled
ElseIf emailAddresses2 Is Nothing Then 'User cancelled
    GoTo ExitHandler
ElseIf emailAddresses.count = 0 Then
    MsgBox "No email addresses were selected for this client.", vbExclamation
Else
    Set newEmail = New Email
    Set newEmail2 = New Email
    With newEmail
        .HtmlBody = _
            "<p>We referred a potential client to you, " & _
            Nz(First_Name & " ", "") & Nz(Last_Name, "") & _
            ", who needs help with an employment matter in " & Nz(State, "your state") & _
            ".  Thank you for any assistance you might be able to provide.</p>"
        Set .MailTo = emailAddresses
        Set .MailTo = emailAddresses2
        .Cc.Add "kjghkjgh@ihiu.com"
        .Subject = "Potential"
        .Send
    End With
End If End Sub
4

2 回答 2

1

我的程序读取人员列表,如果他们被标记为维护,例如,它会提取一个查询并为该查询设置一个记录集:

strSQL = "SELECT Email FROM Employees WHERE '" & department & "' = Department"
Debug.Print strSQL 'I do this so I can see what my query is in the immediate window
Set myR = CurrentDb.OpenRecordset(strSQL)

然后,当创建记录集时,我循环遍历与查询中的条件匹配的每条记录,并将其添加到olTo电子邮件中。

Do While Not myR.EOF
    Debug.Print myR!Email 'print the email of the record I'm on
    Set appOutlookRec = .Recipients.Add(myR!Email)
    appOutlookRec.Type = olTo
    myR.MoveNext
Loop

但为了更具体地回答您的问题,我将添加两个IF语句,首先检查是否Referral_To_ID_2为空。

If Referral_To_ID_2 = Null Then 'or if its a string then If Len(Me.Notes & vbNullString) = 0
    'do nothing
Else
    Set emailAddresses2 = GetEmails(referralId:=Referral_To_ID_2)
End if

然后把第二个放在你的位置Set .MailTo

If emailAddresses2 = Nothing then 'I think that's how that would go, not 100%
    'do nothing
Else
     Set .MailTo = emailAddresses2
End if

不,请知道我不是大师。我试图将它与我所掌握的知识拼凑起来,所以如果有人阅读了这篇文章并且可以更好地修改它,那么就拥有它。让我知道事情的后续。

于 2013-10-14T19:39:34.703 回答
0

因为我需要它来打开两个单独的 Outlook 窗口,所以我基本上必须从第一个引荐中复制并粘贴第二个引荐的代码,而第二个引荐在未选择时不显示错误,这就是我想出的它正在工作:

If IsNull(Referral_To_ID_2) Then On Error Resume Next Else Set emailAddresses2 = GetEmails(referralId:=Referral_To_ID_2) End If 感谢大家的意见!

于 2013-10-16T13:49:21.283 回答