This script will move email from Joe Smith and Wally Gator to the _ALERTS mail box.
It is slow. I think it reads through the whole inbox (which is huge).
Would there be a way to click on the "Unread" filter in Outlook and have the script look at unread emails?
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("_ALERTS")
Dim varSearchTerm As Variant: For Each varSearchTerm In Array("Joe Smith", "Wally Gator")
Set myItem = myItems.Find("[SenderName] = '" & varSearchTerm & "'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
Next
End Sub
You're looking for the filter item [UNREAD] = {TRUE/FALSE}
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("_ALERTS")
Dim varSearchTerm As Variant: For Each varSearchTerm In Array("Joe Smith", "Wally Gator")
Set myItem = myItems.Find("[SenderName] = '" & varSearchTerm & "' AND [UNREAD] = TRUE")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
Next
End Sub
Related
I am trying to move sent mail from my regular Sent Items standard folder to two separate folders in Outlook (365). On the left in my Folder Pane I have my email 'main#domain.com', 'Online Archive - main#domain.com' (an Online Archive for more storage similar to a PST I guess) and then a shared mailbox 'secondary#domain.com'.
One of the backup folders is in my Online Archive and the other backup folder is a shared mailbox. Here's the VBA code I have so far. Ideally I would like it to run each time an email is sent/appears in the Sent Items so I think I could use WithEvents somehow but I am okay to run the macro on an as needed basis.
When I run the code none of the mail moves so I think the issue is something with how I am selecting the filtered mail items to move.
Sub MoveItems()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim mySource As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set mySource = myNameSpace.GetDefaultFolder(olFolderSentMail)
Set myItems = mySource.Items
Set myDestFolder = Outlook.Session.Folders("Online Archive - main#domain.com").Folders("Backup")
Set myItem = myItems.Find("[SenderEmailAddress] = 'main#domain.com'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
Set myItem = myItems.Find("[SenderEmailAddress] = 'secondary#domain.com'")
Set myDestFolder = Outlook.Session.Folders("secondary#domain.com").Folders("SecondaryBackup")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
Sub MoveItems()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim mySource, myDestFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim myItem As Object
Dim strFilter As String
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set mySource = myNameSpace.GetDefaultFolder(olFolderSentMail)
Set myItems = mySource.Items
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:fromname" & Chr(34) & " like '%Main Display Name%'"
Set myDestFolder = Outlook.Session.Folders("Online Archive - main#domain.com").Folders("Backup")
Set myItem = myItems.Find(strFilter)
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:fromname" & Chr(34) & " like '%Shared Box Display Name%'"
Set myDestFolder = Outlook.Session.Folders("Shared Box Display Name").Folders("Backup")
Set myItem = myItems.Find(strFilter)
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
You may change to senderName if senderEmailAddress is not in SMTP format.
Sub MoveItems_senderName()
Dim mySource As Folder
Dim myDestFolder As Folder
Dim myItems As Items
Dim myItem As Object
Set mySource = Session.GetDefaultFolder(olFolderSentMail)
'mySource.Display
Set myItems = mySource.Items
Set myDestFolder = Session.Folders("Online Archive - main#domain.com").Folders("Backup")
Debug.Print "senderName: " & senderName
Set myItem = myItems.Find("[SenderName] = 'text from immediate pane'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
The following is supposed to launch a popup folder picker, and then move the current item to the selected folder.
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim mySubFolder As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set mySubFolder = myNameSpace.PickFolder
Set myDestFolder = myInbox.Folders(mySubFolder)
Set myItem = GetCurrentItem()
myItem.Move myDestFolder
End Sub
I am getting a Type Mismatch on the line
Set myDestFolder = myInbox.Folders(mySubFolder)
That line should be Set myDestFolder = mySubFolder
You may wanna also use If mySubFolder Is Nothing Then Exit Sub just in case user decides to cancel the myNameSpace.PickFolder so you don't get run-time error
Option Explicit
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim mySubFolder As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set mySubFolder = myNameSpace.PickFolder
If mySubFolder Is Nothing Then Exit Sub
Set myDestFolder = mySubFolder
Set myItem = GetCurrentItem()
myItem.Move myDestFolder
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
IsNothing Function
IsNothing returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False.
How can I go through this "indefinite 5-10..." list of senders and delete their messages:
mySenders =" Dan Wilson, Tom Hanks, Alisa Milano, Jessica Alba, Torrid, Captain America"
The code below works for a single sender.
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myNameSpace.GetDefaultFolder(olFolderDeletedItems)
'how to loop here?
Set myItem = myItems.Find("[SenderName] = 'Kmart'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
little confuse. but to delete msg from multiple sender, Add a Second While .. Wend and modify it to use myItem.Delete
Example:
Tested on Outlook 2010
Option Explicit
Sub DeleteItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
'// loop for each sender
Set myItem = myItems.Find("[SenderName] = 'Dan Wilson'")
While TypeName(myItem) <> "Nothing"
myItem.Delete
Set myItem = myItems.FindNext
Wend
'// Loop Next Sender
Set myItem = myItems.Find("[SenderName] = 'Tom Hanks'")
While TypeName(myItem) <> "Nothing"
myItem.Delete
Set myItem = myItems.FindNext
Wend
Set myItem = myItems.Find("[SenderName] = 'Alisa Milano'")
While TypeName(myItem) <> "Nothing"
myItem.Delete
Set myItem = myItems.FindNext
Wend
' More here
End Sub
You could loop through an an array based on mySenders.
Option Explicit
Sub MoveItems()
Dim myNameSpace As Namespace
Dim myInbox As folder
Dim myDestFolder As folder
Dim myItems As Items
Dim myItem As Object
Dim mySenders() As String
Dim i As Long
Set myNameSpace = GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myNameSpace.GetDefaultFolder(olFolderDeletedItems)
mySenders = Split("Dan Wilson,Tom Hanks,Alisa Milano,Jessica Alba,Torrid,Captain America", ",")
For i = LBound(mySenders) To UBound(mySenders)
Debug.Print i & " - " & mySenders(i)
Set myItem = myItems.Find("[SenderName] = """ & mySenders(i) & """")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
Next
End Sub
You will need the exact names.
Sub display_SenderName()
Dim currItem As Object
Select Case ActiveWindow.Class
Case olExplorer
' The active window is a list of messages (folder)
' There might be several selected messages
' Here only one is processed
Set currItem = ActiveExplorer.Selection(1)
Debug.Print currItem.Subject
Debug.Print currItem.senderName
Case olInspector
Set currItem = ActiveInspector.currentItem
Debug.Print currItem.Subject
Debug.Print currItem.senderName
End Select
End Sub
So I am trying to create a Macro that will search my e-mails based on a piece of information on an access form I know I am close but I cannot seem to figure out the final piece
Private Sub btnEMAIL_Click()
Dim strID As String, strMessages As String
Call Outlook_open 'CHECKS TO SEE IF OUT LOOK IS OPEN
Dim myOlApp As Object
Set myOlApp = CreateObject("Outlook.Application") 'Creates outlook object
strID = PayeeID.Value 'this is a value on the form
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim blnfound As Boolean
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.Folders("HQP Field Compensation").Folders("Inbox")
Set myitems = myInbox.Items
Set mySearch = AdvancedSearch(Scope:=myInbox,Filter:="urn:schemas:mailheader:subject= '" & strID & "'")
Set myResults = mySearch.Results
If myResults.Count > 0 Then
For intCounter = 1 To myResults.Count
myResults.Item(intCounter).Display 'Should display the relevant e-mail
Next intCounter
End If
End Sub
AdvancedSearch is asynchronous/ Since you are only searching through the Inbox, use Items.Restrict or Items.Find/FindNext
set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
set myItems = myInbox.Items
set myItem = myItems.Find("[Subject]='" & strID & "'")
while Not (myItem Is Nothing)
myItem.Display
set myItem = myItems.FindNext
wend
I am new to VBA and need your help on a rule that will search the subject of an email and if a specific string "LSC_" found in the subject eg: LSC_IND_TATA and the default naming convention is LSC_XXX_XXX or [LSC_XXX_XXX] then the message is moved to that named sub-folder or a newly created sub-folder of LSC.
So the outlook folder structure looks like the below
LSC
-LSC_IND_TATA
-LSC_IND_TATA_02
-LSC_xxx_xxx
Function CheckForFolder(strFolder As String) As Boolean
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
On Error Resume Next
Set FolderToCheck = olInbox.Folders(strFolder)
On Error GoTo 0
If Not FolderToCheck Is Nothing Then
CheckForFolder = True
End If
ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
Set CreateSubFolder = olInbox.Folders.Add(strFolder)
ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
Function SearchAndMove(lookFor As String)
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder
Dim myItem As Object
Dim MyFolder As Outlook.MAPIFolder
Dim lookIn As String
Dim newName As String
Dim location As Integer
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
For Each myItem In olInbox.Items
lookIn = myItem.Subject
If InStr(lookIn, lookFor) Then
location = InStr(lookIn, lookFor)
newName = Mid(lookIn, location)
If CheckForFolder(newName) = False Then
Set MyFolder = CreateSubFolder(newName)
myItem.Move MyFolder
Else
Set MyFolder = olInbox.Folders(newName)
myItem.Move MyFolder
End If
End If
Next myItem
End Function
Sub myMacro()
Dim str As String
str = "LSC_"
SearchAndMove (str)
End Sub
Function CheckForFolder(strFolder As String) As Boolean
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Dim olInbox_Target As Outlook.MAPIFolder ' <---
Dim FolderToCheck As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox_Target = olNS.GetDefaultFolder(olFolderInbox).Folders("LSC") ' <---
On Error Resume Next
Set FolderToCheck = olInbox_Target.Folders(strFolder) ' <---
On Error GoTo 0
If Not FolderToCheck Is Nothing Then
CheckForFolder = True
End If
ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Dim olInbox_Target As Outlook.MAPIFolder ' <---
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox_Target = olNS.GetDefaultFolder(olFolderInbox).Folders("LSC") ' <---
Set CreateSubFolder = olInbox_Target.Folders.Add(strFolder)
ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
Function SearchAndMove(lookFor As String, myitem As mailItem)
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
'Dim olInbox As Outlook.MAPIFolder
Dim olInbox_Target As Outlook.MAPIFolder ' <---
Dim FolderToCheck As Outlook.MAPIFolder
'Dim myitem As Object
Dim MyFolder As Outlook.MAPIFolder
Dim lookIn As String
Dim newName As String
Dim location As Integer
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
'Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
Set olInbox_Target = olNS.GetDefaultFolder(olFolderInbox).Folders("LSC") ' <---
'For Each myItem In olInbox.Items
lookIn = myitem.Subject
If InStr(lookIn, lookFor) Then
location = InStr(lookIn, lookFor)
newName = Mid(lookIn, location)
If Right(newName, 1) = "]" Then
newName = Left(newName, Len(newName) - 1)
End If
If CheckForFolder(newName) = False Then
Set MyFolder = CreateSubFolder(newName)
myitem.Move MyFolder
Else
Set MyFolder = olInbox_Target.Folders(newName)
myitem.Move MyFolder
End If
End If
'Next myItem
End Function
' Choose this in Run a Script
Sub myMacro(itm As mailItem)
Dim str As String
str = "LSC_"
SearchAndMove str, itm
End Sub
' To test
' Manually select an email with an appropriate subject
Sub myMacroTest()
Dim itm As mailItem
Set itm = ActiveExplorer.Selection(1)
myMacro itm
End Sub