如何绕过 blank/empty MailItem.To 属性 上的测试?

How to bypass test on blank/empty MailItem.To property?

当用户未填写 MailItem 对象的 .To 属性 时,如何绕过标准 Outlook 检查功能?

如果 MailItem.To 为空,我会收到一条错误消息。

我的目标是创建一个函数来检查 .To 属性 何时为空以避免 VBA 错误并显示消息框,停止 VBA 脚本直到对象 .To 已正确填充。

Dim objMail As Outlook.MailItem
Dim objWho As String

objWho = objMail.To

If objMail.To xxxEpmty.something Then
    MsgBox ("Object To is blank")
Else
    objWho = objMail.To

Option Explicit
Public Sub Example()
    Dim objMail As Outlook.MailItem
    Set objMail = Application.CreateItem(0)

    Dim objWho As String
        objWho = ""

        If objWho = vbNullString Then
            MsgBox "Error objwho is blank"
            Exit Sub
        End If

    With objMail
        .To = objWho
        .Send
    End With

End Sub

您还应该使用 Resolve 或 ResolveAll 方法根据地址簿解析收件人。

Recipient.Resolve method (Outlook)

Recipients.ResolveAll method (Outlook)

收件人 属性 是一个字符串。

Sub forceTo()

    Dim objMail As MailItem
    Dim strWho As String

    ' first create mail
    Set objMail = ActiveInspector.CurrentItem

    If objMail.To = "" Then
        MsgBox ("To field is blank")
    Else
        strWho = objMail.To
        MsgBox ("To field contains this text: " & strWho)
    End If

End Sub