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
237 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
ID: 22820814
Sharath,

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

Author Comment

by:bsharath
ID: 22820877
We will have to define it...
0
 
LVL 11

Author Comment

by:bsharath
ID: 22820878
We will have to define it...
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22820908
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
ID: 22820936
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
ID: 22820937
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
ID: 22821045
"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
ID: 22821478
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
ID: 22822333
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
ID: 22822360
Ok thank u....
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22827559
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
ID: 22828220
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
ID: 22828221
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
ID: 22829422
"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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Author Comment

by:bsharath
ID: 22842296
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
ID: 22846143
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
ID: 22856015
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
ID: 22856793
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
ID: 22857341
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
ID: 22857457
That's not a valid path.  Paths use backslashes, not forward slashes.
0
 
LVL 11

Author Comment

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

Expert Comment

by:David Lee
ID: 22857549
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
ID: 22857560
I get
Run time error 434
Object does not support this property  or method...
0
 
LVL 11

Author Comment

by:bsharath
ID: 22857563
When clicked Debug goes here

        Debug.Print olkMsg.SenderEmailAddress
0
 
LVL 76

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

by:bsharath
ID: 22857789
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
ID: 22860744
Any view
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Find out what you should include to make the best professional email signature for your organization.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

24 Experts available now in Live!

Get 1:1 Help Now