PDA

View Full Version : wildcard for myItems.Find ?



bdsii
12-01-2010, 12:37 PM
Hello all.....I have VBA code that moves emails to specific PST folders based on exact email addresses. Is there a wildcard that I can use so that any email coming from a specific domain can be routed to that folder ?
It needs to be able to handle differing length email addresses.

I tried the following line to make it work with the * in front of the @domain but no luck, that was just a WAG on my part.



Set myItem = myItems.Find("[SenderEmailAddress] = '*@domain-goes-here.com'


Any ideas from the experts ?


thanks !

Charlize
12-02-2010, 03:53 PM
Not tested but something with instr ...
'compare the full address with partial string
'if match found, the position of the string will be greater then zero
Set myItem = myItems.Find(InStr(1, [SenderEmailAddress], "@domain-goes-here.com") > 0)Charlize

Charlize
12-03-2010, 08:15 AM
After looking at the help files, it seems to me that you can't use the restrict and find method to filter on a partial string. You'll need the advanced search option but the help files suggest to loop through and use instr for partial strings. Here a little example. First select a bunch of emails and alter the domain you are looking for.
Sub count_mails_from_at_address()
Dim myitems As Object, myitem As Object
'select a bunch of emails before running this
Set myitems = ActiveExplorer.Selection
MsgBox myitems.Count
For Each myitem In myitems
If myitem.Class = olMail Then
If InStr(1, myitem.SenderEmailAddress, "@domain.you.want.com") > 0 Then
MsgBox "This email comes from the desired domain" & vbCrLf & _
"from : " & myitem.SenderEmailAddress & vbCrLf & _
"subject: " & myitem.Subject
End If
End If
Next myitem
End SubCharlize

bdsii
12-03-2010, 02:14 PM
Thanks for the help Charlize......the code below was my original code and I was hoping to figure out a way to tweak the code below to move the emails that matches the domain as the code 'walks down" each item in the Inbox. I took the code you provide and figured out how to add a couple of things to cause the email to move to a specific folder but they have to all be selected first and then I received an error, something to do with the Admin on our system has set a limit on how many emails can be opened. It seems that each time it touches the email it must open them and then move and close them or something ?

Anyway, based on the entire code below, do you see a way to move the InStr line into my original code so that it could be executed without selecting ?

Any other ideas on how to possibly accomplish this ?

Thanks !


Sub MoveItems_BasedOnDomain()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
Set myDestFolder = Outlook.Session.Folders("Top Email Folder").Folders("secondary email folder")
Set myitem = myitems.Find("[SenderEmailAddress] = '*@domaingoeshere.com AND [Unread] = 'false' ")
While TypeName(myitem) <> "Nothing"
myitem.Move myDestFolder
Set myitem = myitems.FindNext
Wend
End Sub

Charlize
12-06-2010, 01:46 AM
Sub MoveItems_BasedOnDomain()
'why creating a new instance of outlook if you're already in outlook ?
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
'get default inbox
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
'put everything of the inbox in this variable
Set myitems = myInbox.Items
'where is the folder located to move the messages to
'same level as inbox then use
'Set myDestFolder = myInbox.Parent.Folders("secondary email folder")
'sublevel of inbox then use (if i'm not mistaken)
'Set myDestFolder = myInbox.Folders("secondary email folder")
Set myDestFolder = myInbox.Parent.Folders("secondary email folder")
For Each myitem In myitems
If myitem.Class = olMail Then
If InStr(1, myitem.SenderEmailAddress, "@domain.you.want.com") > 0 And _
myitem.UnRead = False Then
myitem.Move myDestFolder
'do we need to save the move ?
myitem.Save
End If
End If
Next myitem
'quit the new session
myOlApp.Quit
'clean memory
Set myOlApp = Nothing
End SubCharlize