Utilizing Outlook Events From Excel

后端 未结 1 1556
太阳男子
太阳男子 2020-12-06 22:05

I\'m attempting to utilize the \"ItemAdd\" Event MSDN Link Here from the Outlook Object Model in a Excel macro; however, I haven\'t been able to find a way to utilize events

相关标签:
1条回答
  • 2020-12-06 22:22

    You can do this, but you need to use a Class module in Excel to accomplish it.

    Class Module - "myOutlook" or call it whatever you want.

     Private WithEvents myItems As Outlook.Items
    
     Private Sub Class_Initialize()
    
         Dim oNS As Namespace
         Dim myOL As Outlook.Application
    
         Set myOL = New Outlook.Application
         Set oNS = myOL.GetNamespace("MAPI")
         Set myItems = oNS.GetDefaultFolder(olFolderInbox).Items
         'Set this equal to the folder you wish to use this on
    
     End Sub
    
     Private Sub myItems_ItemAdd(ByVal Item As Object)
    
          Debug.Print "Got_EMAIL!!!"
    
     End Sub
    

    Then, in a regular module do this:

     Dim myOutlook As myOutlook
    
     Sub TestSub()
    
          Set myOutlook = New myOutlook
    
      End Sub
    

    Once you initialize the instance of your user defined class, the events will be caught by it.

    Obviously, you will need to set the "myItems" object to be linked to the correct inbox. For mine, it's just linked to my default mailbox which was easiest for testing.

    0 讨论(0)
提交回复
热议问题