Hi all - with some samples from this forum, I have the following code. I want to copy mail as it enters a folder (for my test I am using the inbox, but eventually want this to be for the Sent and Deleted items) over to another folder. I am getting an error "91 - Object variable or With block variable not set". I have a folder named "test" in the folder hierarchy at the same level as Inbox (in the code I replaced my actual email address with 'emailaddresshere', but what I was using was the name at the top of my folder list in Outlook). This is for Outlook 2016. Any ideas what I have wrong?
Thank you very much for any help!
-randy
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' (1) default Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
' (2) only act if it's a MailItem
Dim Msg As Outlook.MailItem
Dim fldr As Outlook.MAPIFolder
If TypeName(item) = "MailItem" Then
Set fldr = Outlook.Session.Folders("emailaddresshere").Folders("Test") ' problem is on this line I think...
Msg.Copy fldr
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub