Solved

Mail id's in a txt file need to move all mails from that person to another folder.

Posted on 2008-10-28
29
234 Views
Last Modified: 2010-04-08
Hi,

Mail id's in a txt file need to move all mails from that person to another folder.
I have 300+ Mail id's like
Sharath@plc.com
etc
Want an Outlook Macro that goes to each email id searches them in a specific folder and moves that mail 1 or more from the person to another folder specified. So at last i have just have the mails of those 300+

Regards
Sharath
0
Comment
Question by:bsharath
  • 17
  • 12
29 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Sharath,

How will the script know what folder it should move the messages to?
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
We will have to define it...
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
We will have to define it...
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Meaning that you'll put them in the file with the addresses, or you want to be prompted each time, or ...?  I can't put the script together without knowing how to handle this.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
The from and To folders has to be mentioned in the Macro. The email id's are in a txt file
>>Mail id's in a txt file need to move all mails from that person to another folder.


The mails in a folder have to be checked quering the mail id's in a txt file. Then move the mails to another folder when found. Both the mails where it resides and the mails to be moved has to be mentioned in the macro
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
The from and To folders has to be mentioned in the Macro. The email id's are in a txt file
>>Mail id's in a txt file need to move all mails from that person to another folder.


The mails in a folder have to be checked quering the mail id's in a txt file. Then move the mails to another folder when found. Both the mails where it resides and the mails to be moved has to be mentioned in the macro
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
"The from and To folders has to be mentioned in the Macro."
Mentioned how?  Are you saying that the script should use a case statement, something like this

    Select Case Address
        Case "PersonA@somewhere.com"
            Item.Move SomeFolder
        Case "PersonB@somewhere.com"
            Item.Move SomeOtherFolder
        Case "PersonA@somewhere.com"
            Item.Move AnotherFolder
    End Select

where there is a Case statement for every addressee in the file?  If so, then you don't need the file.  Otherwise, the destination folders have to follow some pattern, for example the folder name is the address and they are all at the same level in the folder hierarchy.  The only other solutions are to pair the folder name with the address in the input file, something like this

    PersonA@somewhere.com,SomeFolder
    PersonB@somewhere.com,SomeOtherFolder
    PersonC@somewhere.com,AnotherFolder

or prompting you for each address.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
No..

I want the macro to query only 1 Folder in the outlook and move them to another folder when found the mail id from the txt file.

I have the txt files as
    PersonA@somewhere.com
    PersonB@somewhere.com
    PersonC@somewhere.com

Need to search a folder call "All Mails" and move found mails to 'Retrieved" folder.

Sorry for not being clear...
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Oh, so you want them all moved to one folder.  Sorry, I was thinking you wanted them sorted into separate folders.  One folder is no problem.  I'll get the code posted by the end of the day.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Ok thank u....
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Ok, here you go.
Sub MoveSomeMessages()

    Dim olkSrcFolder As Outlook.MAPIFolder, _

        olkDstFolder As Outlook.MAPIFolder, _

        olkMsg As Outlook.MailItem, _

        objFSO As Object, _

        objFile As Object, _

        dicAddresses As Object, _

        varBuffer As Variant, _

        intIndex As Integer

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Change the file name and path on the following line'

    Set objFile = objFSO.OpenTextFile("C:\eeTesting\Addresses.txt")

    Set dicAddresses = CreateObject("Scripting.Dictionary")

    Do Until objFile.AtEndOfStream

        varBuffer = objFile.ReadLine

        dicAddresses.Add LCase(varBuffer), LCase(varBuffer)

    Loop

    Set olkSrcFolder = Application.ActiveExplorer.CurrentFolder

    'Change the folder path on the following line to that of the destination folder'

    Set olkDstFolder = OpenOutlookFolder("Mailbox - Doe, John/Some Folder")

    For intIndex = olkSrcFolder.Items.Count To 1 Step -1

        Set olkMsg = olkSrcFolder.Items.Item(intIndex)

        Debug.Print olkMsg.SenderEmailAddress

        If dicAddresses.Exists(LCase(olkMsg.SenderEmailAddress)) Then

            olkMsg.Move olkDstFolder

        End If

    Next

    objFile.Close

    Set objFile = Nothing

    Set objFSO = Nothing

    Set olkMsg = Nothing

    Set olkDstFolder = Nothing

    Set olkSrcFolder = Nothing

End Sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get this

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Ambiguous name detected: OpenOutlookFolder
---------------------------
OK   Help  
---------------------------
I set this line as this
Set olkDstFolder = OpenOutlookFolder("Mailbox - Sharath,out")

Sharath is the mailbox name and "Out" the destination folder name below Inbox
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get this

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Ambiguous name detected: OpenOutlookFolder
---------------------------
OK   Help  
---------------------------
I set this line as this
Set olkDstFolder = OpenOutlookFolder("Mailbox - Sharath,out")

Sharath is the mailbox name and "Out" the destination folder name below Inbox
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
"Ambiguous name" means that you have two or more procedures with the same name.  You'll have to check through your code and see if you have more than one OpenOutlookFolder function/subroutine.  I didn't include that function when I posted the code because I was sure you already have it.

"Set olkDstFolder = OpenOutlookFolder("Mailbox - Sharath,out")"
That isn't a valid folder path.  Folders paths in Outlook are just like those in the file system.  They have to detail all the folders from the root to the folder you want and each folder name is separated by a \ character.  So, the path would be

Set olkDstFolder = OpenOutlookFolder("Mailbox - Sharath\Inbox\Out")

or something like that.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Author Comment

by:bsharath
Comment Utility
I am trying this code from an Machine that does not have any code in outlook. Freash machine.
Put in the above code in a module in outlook 2007
When run get this.
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Sub or Function not defined
---------------------------
OK   Help  
---------------------------

I have changed this line as
Set olkDstFolder = OpenOutlookFolder("Mailbox - sharath re/Inbox/out")

Is there some thing i am doing wrong...
I tried both \ and \ as seperation of the folders.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Sharath,

You need to copy the subroutine called OpenOutlookFolder to that machine too.  I didn't include it in the code I posted above because I know you already have it and I didn't want to create a problem with a duplicate.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Hi,

Can you include it as this machine where i will use does not have any code...
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
Sure.
Function IsNothing(obj)

  If TypeName(obj) = "Nothing" Then

    IsNothing = True

  Else

    IsNothing = False

  End If

End Function
 

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder

    Dim arrFolders As Variant, _

        varFolder As Variant, _

        olkFolder As Outlook.MAPIFolder

    On Error GoTo ehOpenOutlookFolder

    If strFolderPath = "" Then

        Set OpenOutlookFolder = Nothing

    Else

        If Left(strFolderPath, 1) = "\" Then

            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)

        End If

        arrFolders = Split(strFolderPath, "\")

        For Each varFolder In arrFolders

            If IsNothing(olkFolder) Then

                Set olkFolder = Session.Folders(varFolder)

            Else

                Set olkFolder = olkFolder.Folders(varFolder)

            End If

        Next

        Set OpenOutlookFolder = olkFolder

    End If

    On Error GoTo 0

    Exit Function

ehOpenOutlookFolder:

    Set OpenOutlookFolder = Nothing

    On Error GoTo 0

End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get a run time error 13
When debug goes here
Set olkMsg = olkSrcFolder.Items.Item(intIndex)

I have this line as this
Set olkDstFolder = OpenOutlookFolder("Mailbox - Sharath BH/Inbox/closed nas")
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
That's not a valid path.  Paths use backslashes, not forward slashes.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I even tried that get the same error. Before i posted my comments
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Ok, but I can only go by what you post, not what you've tried that I don't know about.

The error is a type mismatch.  It means one of the items in the folder isn't an email.  It's a receipt, meeting request, etc.  The simplest way to fix that is to change how olkMsg is declared on line #4 of the code from

        olkMsg As Outlook.MailItem, _

to

        olkMsg As Object, _
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get
Run time error 434
Object does not support this property  or method...
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
When clicked Debug goes here

        Debug.Print olkMsg.SenderEmailAddress
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Delete that line.  It was left over from testing.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get the same error for this line now
        If dicAddresses.Exists(LCase(olkMsg.SenderEmailAddress)) Then
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Then you have to find the non-email item in the folder and remove it.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Sorry mistakenly closed this one...

I checked the folder no other items except mails are there in.
I sort them by type and could find just mail items...
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Any view
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now