I have the following code in Outlook to save attachments if an email is received that meets certain criteria.
The code works, however anytime I start Outlook I receive
Run-time error '424': Object Required
The debugger takes me to:
Set objItems = objInbox.Items.
In addition, the code only works when I have my laptop on and Outlook running. How can I rewrite this code so that it works whether or not my computer is on and Outlook is running? These are emails being sent to a group email box.
Public WithEvents objItems As Outlook.Items
Private Sub Application_Startup()
Set objItems = objInbox.Items
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
Dim objTaskRequest As Outlook.TaskRequestItem
End Sub
Sub BBDLPRICEMailMessageRule(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd_")
sSaveFolder = "N:\Applications\APX\Import\BB_DL_Prices"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & "\" & oAttachment.DisplayName
Next
End Sub
You need to initialize the objInbox
object in the code:
Dim myNamespace As Outlook.NameSpace
Private Sub Application_Startup()
Set myNamespace = Application.GetNamespace("MAPI")
Set objInbox = myNamespace.GetDefaultFolder(olFolderInbox)
Set objItems = objInbox.Items
End Sub