Reputation: 595
I've got a macro that moves each e-mail in a subfolder
to inbox, and works perfectly!
But how can I call a macro to that specific e-mail that has been moved?
Macro to move email:
Public Sub Mover_Email()
' // Declare your Variables
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim olNs As Outlook.NameSpace
Dim Item As Object
Dim lngCount As Long
Dim Items As Outlook.Items
On Error GoTo MsgErr
' Set Inbox Reference
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = Application.Session.GetDefaultFolder(olFolderInbox).Folders("1 - Arquivos Temporarios")
Set Items = Inbox.Items
' // Loop through the Items in the folder backwards
For lngCount = Items.count To 1 Step -1
Set Item = Items(lngCount)
Debug.Print Item.Subject
If Item.Class = olMail Then
' // Set SubFolder of Inbox
Set SubFolder = olNs.GetDefaultFolder(olFolderInbox)
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to sub Folder
Item.Move SubFolder
'Call the macro for that email
'************
'Enter the macro here
'************
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set olNs = Nothing
Set Item = Nothing
Exit Sub
'// Error information
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub
I thought that selecting the folder "Inbox" and execute the macro in that e-mail could work, but I don't know how.
If there's some other simple solution, I'd prefer that (like not selecting the Inbox maybe).
Upvotes: 1
Views: 297
Reputation: 12499
Work with NameSpace.PickFolder method (Outlook)
Example
Set Inbox = Application.Session.PickFolder
You could also set your Subfolder
to PickFolder
but move it outside the loop
Example
Option Explicit
Public Sub Mover_Email()
' // Declare your Variables
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim olNs As Outlook.NameSpace
Dim Item As Object
Dim lngCount As Long
Dim Items As Outlook.Items
On Error GoTo MsgErr
' Set Inbox Reference
Set olNs = Application.GetNamespace("MAPI")
Set Inbox = Application.Session.PickFolder
Set Items = Inbox.Items
' // Set SubFolder
Set SubFolder = Application.Session.PickFolder
' // Loop through the Items in the folder backwards
For lngCount = Items.Count To 1 Step -1
Set Item = Items(lngCount)
Debug.Print Item.Subject
If Item.Class = olMail Then
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to sub Folder
Item.Move SubFolder
'Call the macro for that email
'************
'Enter the macro here
'************
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set olNs = Nothing
Set Item = Nothing
Exit Sub
'// Error information
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub
To move selected Email to Inbox try the following
Option Explicit
Public Sub Exampls()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Set Item = ActiveExplorer.selection(1)
Debug.Print Item.Parent
If TypeOf Item Is Outlook.MailItem Then
If Not Item.Parent = Inbox Then
Item.Move Inbox
MsgBox "Item Subject: " & Item.Subject & " Has Been Move to " & Inbox.Name
Else
MsgBox "Item already in " & Item.Parent
Exit Sub
End If
Else
MsgBox "Selection is not MailItem"
End If
End Sub
Upvotes: 0
Reputation: 9179
The reference to the mail is lost in the move.
Create a reference to the moved mail with Set movedItem = …
.
Public Sub Move_first_then_Process_Email()
' // Declare your Variables
Dim Inbox As Folder
Dim SubFolder As Folder
' Dim olNs As NameSpace
Dim Item As Object
Dim lngCount As Long
Dim Items As Items
Dim movedItem As MailItem
' Not when developing
' On Error GoTo MsgErr
' Set Inbox Reference
' Not needed when using Session
' Set olNs = GetNamespace("MAPI")
Set Inbox = Session.GetDefaultFolder(olFolderInbox).Folders("1 - Arquivos Temporarios")
Set Items = Inbox.Items
' // Set target folder
Set SubFolder = Session.GetDefaultFolder(olFolderInbox)
' // Loop through the Items in the folder backwards
For lngCount = Items.Count To 1 Step -1
Set Item = Items(lngCount)
Debug.Print "Subject of Item: " & Item.Subject
If Item.Class = olMail Then
'
' // Mark As Read
Item.UnRead = False
' // Move Mail Item to target folder
' and create a reference to the moved item
Set movedItem = Item.Move(SubFolder)
'Call the macro for moved email
'************
display_Subject movedItem
'************
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set Item = Nothing
Exit Sub
'// Error information
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub
Private Sub display_Subject(ByRef mvItem As Object)
If mvItem.Class = olMail Then
Debug.Print "Subject of movedItem: " & mvItem.Subject
Debug.Print
Else
Debug.Print "Not a mailitem."
End If
End Sub
Upvotes: 1