Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am working on VBA automation to move all emails containing one specific mail Id to the hard drive folder

Below is the code I have got so far but seems it is not returning me any mails in folders. if anyone can suggest where I am going wrong?

Dim OlApp As Outlook.Application
Set OlApp = CreateObject("Outlook.Application")
Dim fldrpath As String
Dim oMails As Outlook.Items
Dim oMail As Object

fldrpath = "\\data\EMAILS\abc@gmail.com"

Dim NS As Outlook.Namespace
Dim Folder As MAPIFolder
Dim Olfolder As Outlook.MAPIFolder

Set NS = OlApp.GetNamespace("MAPI")
Set Olfolder = NS.GetDefaultFolder(olFolderInbox)

' Check subfolder for messages and exit of none found

Set oMails = Olfolder.Items
Set oMail = oMails.Find("[Sendername]= 'abc@gmail.com'")
While TypeName(oMail) <> "Nothing"
oMail.Move fldrpath
Set oMail = oMails.FindNext
Wend


What I have tried:

The above code is written in excel VBA and tried to connect with outlook app and inbox folder.
Posted
Updated 5-Jul-17 3:28am
Comments
Richard Deeming 6-Jul-17 10:18am    
Are you sure you want to search by Sendername, and not SenderEmailAddress?

1 solution

We can't tell you why it's not working because we don't have the data your code is working with. Chances are really good your .Find call can't find anything that matches the string you're giving it, because I really doubt an email address has ' characters in it.

But, asking in a forum for other people to tell you what you're code is doing and what it's working with is not the correct thing to do. The correct thing to do is to run the code under the debugger in VBA and examine the contents of variables so you can understand what the code is doing.

The debugger is not there to debug the code. Its there to debug YOU and you're understanding of the code and the environment its working in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900