|
Re: macro to move messages from Sent Items from specific mailbox address
Private WithEvents sentItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objRecipient As Recipient
Dim objFolder As MAPIFolder
Set objNS = Me.GetNamespace("MAPI")
Set objRecipient = objNS.CreateRecipient(objNS.CurrentUser.Address)
objRecipient.Resolve
If objRecipient.Resolved Then
Set objFolder = objNS.GetDefaultFolder(olFolderSentMail)
Set sentItems = objFolder.Items
Set objFolder = Nothing
Else
MsgBox ("Error: Name Not Resolved - Please Contact Jon P")
End If
Set objRecipient = Nothing
Set objNS = Nothing
End Sub
Private Sub sentItems_ItemAdd(ByVal Item As Object)
Dim objItem As Outlook.MailItem
Dim objNS As NameSpace
Const g_PR_SMTP_ADDRESS_W = &H39FE001F
If (TypeOf Item Is Outlook.MailItem) Then
Set objItem = Item
Set objSession = CreateObject("MAPI.Session")
objSession.Logon "", "", False, False
strEntryID = objItem.EntryID
strStoreID = objItem.Parent.StoreID
Set objMessage = objSession.GetMessage(strEntryID, strStoreID)
strAddress = objMessage.Sender.Address
If Not InStr(strAddress, "@") Then
On Error Resume Next
strAddress = objMessage.Sender.Fields(g_PR_SMTP_ADDRESS_W).Value
End If
objSession.Logoff
Set objMessage = Nothing
Set objSession = Nothing
Select Case True
Case InStr(LCase(strAddress), "jon.p@xxx.com")
strMailBox = "Mailbox - Jon P"
Case Else
strMailBox = ""
End Select
Set objNS = Application.GetNamespace("MAPI")
Set CDS_SentItems = objNS.Folders(strMailBox).Folders("Sent Items")
objItem.Move CDS_SentItems
Set CDS_SentItems = Nothing
Set objNS = Nothing
Set objItem = Nothing
End If
End Sub
|