0

I am trying to define a MailItem variable in vba in Outlook 2013. However, every time I type Dim mail AS MailItem the MailItem gets updated to mailItem. As I understand, this is not a correct type. When I try MsgBox TypeName(mail), it shows Nothing.

I have no clue why this is happening. Any help will be really appreciated. Thanks!

The entire code is as follows

Private Sub Items_ItemAdd(ByVal newMail As Object)

    'On Error Resume Next
    On Error GoTo ErrorHandler

    Dim mail As Outlook.mailItem
    If TypeName(newMail) = TypeName(mail) Then        ***<-- I want this if block to execute. But it doesn't!***
        Set mail = newMail
        SaveAttachments (newMail)
    End If
ProgramExit:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub
aakashgupta.0205
  • 647
  • 1
  • 8
  • 23

1 Answers1

0

TypeName will display the type name of a live object. If it's never been initialized (null), you will see "Nothing"

That being said, when exactly do you see the dim statement changed? In the Outlook VBA editor?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I am still not sure of the solution. I have added the code above. Pls help me have a look at it! – aakashgupta.0205 Sep 14 '15 at 02:53
  • VBA is not case sensitive, so either declaration will work. You get "Nothing" from TypeName because the variable has not been initialized. This is perfectly fine. You should change "If TypeName(newMail) = TypeName(mail)" to "If TypeName(newMail) = "MailItem" " – Dmitry Streblechenko Sep 14 '15 at 06:36