I'm trying to write a macro to automatically save certain reports we get via email at work. Another department has something very similar in place, so I can re-use a lot of their code. However, they couldn’t really explain to me what certain parts of the code do and googling it wasn't particularly helpful.
If it helps, I’ll post the whole code, I left it out because a) I’d have to edit some things (names etc) and b) I don’t think it’s relevant to my problem.
The point of the code is that on startup the macro starts; every new email gets then checked (by sender; subject line; attachments). I struggle understanding the startup-part of the code because I’m not that experienced with VBA in general and have never used VBA in Outlook.
Option Explicit
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Folders("reports").Items
Set objNS = Nothing
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
If Item.Attachments.Count > 0 Then
….
End Sub
Questions:
Private WithEvents olInboxItems As Items
- What does Private WithEvents
mean or do? I know about events in VBA, like opening a workbook, but I couldn’t figure out what was meant with this line.
NameSpace
: Simply put, what is a NameSpace? What I read was that in Outlook there’s a different structure to the object model compared to say Excel, the NameSpace is just below the application level. If that’s correct, then I guess that’s good enough for me, but if anyone has a neat explanation, I’d appreciate it.
Why would you Set objNS = Nothing
?