1

I've had an Outlook 2016 VBA macro running for a year to check emails arriving in my inbox. Today, following installation of Windows 10 updates, I get a type mismatch error when this macro runs. The error line is the Set olNs = Application.GetNamespace("MAPI") line below:

Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder
    Dim olRecip As Recipient
    Dim dt As String
    Dim strFile_Path As String

    dt = Format(CStr(Now), "yyyy_mmm_dd_hh_mm")
    strFile_Path = "d:\temp\parking.log"
    Open strFile_Path For Append As #1
    Write #1, dt & " " & "Application_Startup() triggered"
    Close #1


    Set olNs = Application.GetNamespace("MAPI")
    Set olRecip = olNs.CreateRecipient("me@gmail.com")
    Set Inbox = olNs.Folders("me@gmail.com").Folders("Inbox")
    Set Items = Inbox.Items
End Sub

Any idea how I can fix this?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
Ian M
  • 567
  • 8
  • 33

2 Answers2

0

First of all, you need to make sure the COM references are set correctly.

enter image description here

You may try to run the code without setting a local variable:

Private Sub Application_Startup() 

 MsgBox "Welcome, " & Application.GetNamespace("MAPI").CurrentUser 

 Application.ActiveExplorer.WindowState = olMaximized 

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks - my References were also set exactly as per your image. I tried removing the explicit declaration of the Namespace variable (olNs), and that seems to work around the problem. Has the object type returned by Application.GetNameSpace changed? – Ian M Jun 12 '20 at 09:35
  • Nothing has been changed so far. It seems your machine is corrupted a bit - windows registry keys were corrupted. – Eugene Astafiev Jun 12 '20 at 11:33
0

I just ran into this issue (COM add-ins were fine) and as stated, removing the explicit declaration seems to fix the issue (you can also Dim the Namespace as an Object instead of Outlook.Namespace).

As a quick reference for anyone else I used the following code to bypass the issue:

With Application.GetNamespace("MAPI")
    Dim Inbox As Outlook.MAPIFolder: Set Inbox = .GetDefaultFolder(olFolderInbox)
    Dim Junk As Outlook.MAPIFolder: Set Junk = .GetDefaultFolder(olFolderJunk)
End With
Tragamor
  • 3,594
  • 3
  • 15
  • 32