1

How do I bypass standard Outlook check function when user doesn't fill the MailItem object's .To property?

If MailItem.To is blank I receive an error message.

My goal is to create a function to check when the .To property is blank to avoid VBA error and show a msg box, stop VBA script until object .To is correctly filled.

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Novice
  • 363
  • 10
  • 32

2 Answers2

1

Use vbNullString if its blank

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

You should also use Resolve or ResolveAll methods to resolve the Recipient against the address book.

Recipient.Resolve method (Outlook)

Recipients.ResolveAll method (Outlook)

0m3r
  • 12,286
  • 15
  • 35
  • 71
1

The To property is a string.

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
niton
  • 8,771
  • 21
  • 32
  • 52