Reputation: 15
I have Outlook code which checks specific subject email in shared mailbox (Inbox) and records the email body data in Excel (yet to add code) and should move the email to MIAL folder.
I get an error while moving emails to MIAL folder.
"Variable not defined"
Folder (MIAL) was created manually. When I code to move emails to default folder like "Drafts" or "Sent Folder" its working.
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim Recip As Outlook.Recipient
Set Recip = olNs.CreateRecipient("[email protected]") 'update email
Dim SharedInbox As Outlook.Folder
Set SharedInbox = olNs.GetSharedDefaultFolder(Recip, _
olFolderInbox) 'Inbox
Dim Movefolder As Outlook.Folder
Set Movefolder = olNs.GetSharedDefaultFolder(Recip, _
olFolderMIAL) 'Folder
Dim Item As Outlook.MailItem
For Each Item In SharedInbox.Items
'If (Item.subject = "TSP") Then
Debug.Print Item.subject
Item.Move Movefolder
'End If
Next
End Sub
Upvotes: 1
Views: 815
Reputation: 12497
Move Folder should be
Dim Movefolder As Outlook.folder Set Movefolder = SharedInbox.Folders("MIAL")
Upvotes: 0
Reputation: 9199
There are specific default folders that can be described in a similar manner to olFolderInbox to be used as shortcuts.
Manually created folders are referenced the long way.
One way to reference a folder at the same level as an inbox is to navigate the folder tree from that inbox to the mailbox then back down again.
Set Movefolder = SharedInbox.Parent.Folders("MIAL")
Upvotes: 0