-
Jun 12th, 2018, 07:52 AM
#1
Thread Starter
New Member
Search outlook inbox for emails matching from alias
I have got this code below working by editing something off the web.
It searches for emails with 2 defined [SenderName]s and moves those emails to a PST folder location
2018\inbox
So far so good.
Now I want to search and move emails from a defined email alias (e.g. info@meetup.com) to the same destination.
The code fragment which seems relevant is this:
Code:
Dim myItems As Outlook.Items
..
Set myItem = myItems.Find("[SenderName] = 'Gumtree Australia'")
I went looking in what I understand to be the Outlook application object model to the Items.find method here:
Office VBA Reference > Outlook VBA > Object model > Items Object (Outlook) > Items.Find Method (Outlook)
But this documentation is not adequate for me to work out what to do.
From the code fragment above it seems one can abstract the following form:
Set myItem = myItems.Find("[VariableName] = 'VariableAssignment'")
So maybe in my case maybe
VariableName = FromEmailAlias; and
VariableAssignment = info@meetup.com
To give
Set myItem = myItems.Find("[FromEmailAlias] = 'info@meetup.com'")
or some such code. I dont know.
Any suggestions greatly appreciated
The existing working macro is below
Code:
' 002
' 003
' 004 Add second criteria
' 005 3rd citeria "From"
'
Sub Move_to_Fixed_SubFolder_005()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
'Got this from Move_Selected_To_Fixed_SubFolder_001.vbs
' START
Dim moveToFolder As Outlook.MAPIFolder
' END
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
' Set the myInbox folder variable to the outlook default folder (i.e. hotmail inbox)
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
'Got this from Move_Selected_To_Fixed_SubFolder_001.vbs
' START
Set moveToFolder = myNameSpace.Folders("2018").Folders("Inbox")
' END
Set myDestFolder = myInbox.Folders("Enq")
Set myItem = myItems.Find("[SenderName] = 'Gumtree Australia'")
While TypeName(myItem) <> "Nothing"
' myItem.Move myDestFolder
myItem.Move moveToFolder
Set myItem = myItems.FindNext
Wend
Set myItem = myItems.Find("[SenderName] = 'Barchart Partner'")
While TypeName(myItem) <> "Nothing"
' myItem.Move myDestFolder
myItem.Move moveToFolder
Set myItem = myItems.FindNext
Wend
Set myItem = myItems.Find("[From] = 'meetup.com'")
While TypeName(myItem) <> "Nothing"
' myItem.Move myDestFolder
myItem.Move moveToFolder
Set myItem = myItems.FindNext
Wend
End Sub
-
Jun 12th, 2018, 04:39 PM
#2
Re: Search outlook inbox for emails matching from alias
to get the email address of the sender
look at the msg.replyrecipients(msg.sendername).addressentry.address
note if the sendername is an email address then there maybe no reply recipients, so check the replyrecipients.count first
you can not use replyrecipients as criteria for either the find or restrict methods of the items collection, so you will have to loop through all the items in the inbox, if there is a large number of items, you may be able to restrict by some other criteria, like receivedtime, then just loop through items not processed previously
also note there may be differences in later versions of outlook, but i would expect these suggestions to still work, just there maybe something easier
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 12th, 2018, 05:23 PM
#3
Thread Starter
New Member
Re: Search outlook inbox for emails matching from alias
Hi westconn1
Thank yuou very much for your reply. It is greatly appreciated.
Originally Posted by westconn1
to get the email address of the sender
you can not use replyrecipients as criteria for either the find or restrict methods of the items collection, so you will have to loop through all the items in the inbox, if there is a large number of items, you may be able to restrict by some other criteria, like receivedtime, then just loop through items not processed previously
It would seem then I cannot simply edit the macro I have working and reuse the existing structure and constructs.
Is there anywhere that I can look that will tell me what I can do with the existing macro?
In the lexcon you deploy is there a url or book which lists what criteria that I can use for the find or restrict methods of the item collection?
I know that I can use [SenderName] as a criteria as I have that working.
I was expecting to find alist of criteria in the MSDN URL I posted but it appears not.
Originally Posted by westconn1
to get the email address of the sender
look at the msg.replyrecipients(msg.sendername).addressentry.address
note if the sendername is an email address then there maybe no reply recipients, so check the replyrecipients.count first
Above I think are pointers to an alternative macro structure which I could do what I want to do.
I will use theses fragments to see if I can find another working sample script to play with. Thank you just the clue that I need.
Originally Posted by westconn1
also note there may be differences in later versions of outlook, but i would expect these suggestions to still work, just there maybe something easier
Noted thank you again.
-
Jun 13th, 2018, 04:58 AM
#4
Re: Search outlook inbox for emails matching from alias
n the lexcon you deploy is there a url or book which lists what criteria that I can use for the find or restrict methods of the item collection?
i use the installed vba help file, but later versions no longer come with that
a real quick google found this as nearly the top link https://msdn.microsoft.com/en-us/vba...method-outlook
Remarks
This method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found.
Note If you are using user-defined fields as part of a Find or Restrict clause, the user-defined fields must exist in the folder. Otherwise the code will generate an error stating that the field is unknown. You can add a field to a folder by displaying the Field Chooser and clicking New.
This method cannot be used and will cause an error with the following properties:
| Body| LastFirstNoSpaceCompany| | Categories| LastFirstSpaceOnly| | Children| LastFirstSpaceOnlyCompany| | Class| LastFirstNoSpaceAndSuffix| | Companies| MemberCount| | CompanyLastFirstNoSpace| NetMeetingAlias| | CompanyLastFirstSpaceOnly| NetMeetingAutoStart| | ContactNames| NetMeetingOrganizerAlias| | Contacts| NetMeetingServer| | ConversationIndex| NetMeetingType| | DLName| RecurrenceState| | Email1EntryID| ReceivedByEntryID| | Email2EntryID| RecevedOnBehalfOfEntryID| | Email3EntryID| ReplyRecipients| | EntryID| ResponseState| | HTMLBody| Saved| | IsOnlineMeeting| Sent| | LastFirstAndSuffix| Submitted| | LastFirstNoSpace| VotingOptions| | AutoResolvedWinner| DownloadState| | BodyFormat| IsConflict| | InternetCodePage| MeetingWorkspaceURL| | Permission||
i googled outlook items.restrict help
there were other hits that indicated examples for some of the criteria
Is there anywhere that I can look that will tell me what I can do with the existing macro?
i could be rude here
It would seem then I cannot simply edit the macro I have working and reuse the existing structure and constructs.
i do not see why some small modification, to your existing code, based on the hints i provided would not work fine, use the restrict method to return a subset of the folder items, probably based on after the last time the macro was run, then use for each itm in items to look at each email in the subset
Code:
Set myItems = myInbox.Items
someemail = "info@meetup.com"
Set moveToFolder = myNameSpace.Folders("2018").Folders("Inbox")
Set myDestFolder = myInbox.Folders("Enq")
Set mysubitems = myItems.restrict("[ReceivedTime] >= '" & date - 1 & "'") ' this should create a subset for yesterday to current time, assuming that the macro is run each day, or modify to suit
for each msg in mysubitems
if msg.ReplyRecipients.Count > 0 then
if msg.ReplyRecipients(msg.SenderName).AddressEntry.Address = someemail then msg.Move moveToFolder ' if you want to match a partial email address then use instr
end if
Next
i only typed most of this in the browser window, so may contain typos or code errors and is untested
this can be inserted in the existing macro below the existing line that matches the first line here, it can be used in place of or in addition to the existing criterias, change the target of movetofolder as required
edited code to fix a couple of issues
Last edited by westconn1; Jun 13th, 2018 at 05:29 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 13th, 2018, 05:09 AM
#5
Thread Starter
New Member
Re: Search outlook inbox for emails matching from alias
Hi westconn1
Greatly appreciate further feedback. I will take some time to absorb.
Once again my thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|