Solved

To move email message to another email folder

Posted on 2013-10-22
8
326 Views
Last Modified: 2013-11-07
Hi,

Would you please advice me VBA code to move a message from a outlook folder to another.

I want to move a message which is in another folder as an attached message.
 for example outlook folder from_customer has some email which contain an attachment of a message want to collect the attached message and move to another outlook folder name New_messages.

I have multiple emails that contain such attachment therefore need to do a loop to move all attached messages to another folder mentioned above.

Please advice VBA code to do that.

Thanks
0
Comment
Question by:alam747
8 Comments
 
LVL 70

Expert Comment

by:KCTS
ID: 39594635
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 39594653
DO you want to move the email or the attachment email?

Are you trying to move these as they come in - email rules can do that
or
Are you trying to move them after the fact as a cleanup effort

mlmcc
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39594846
You specifically ask for a macro and the question is in excel zone so I presume you want to run it from excel therefore see the following but modify the folders as appropriate as well as the sender email address:

Sub q1()
Dim olkApp As Object
Dim sourceFolder As Object
Dim destFolder As Object
Dim myItems As Object
Dim itm As Object
Dim str As String
Dim myRestrict As Object
Dim itemCount As Integer
Const olkFolderInbox As Integer = 6

    Set olkApp = CreateObject("outlook.application")
    Set sourceFolder = olkApp.Session.GetDefaultFolder(olkFolderInbox)
    Set destFolder = olkApp.Session.GetDefaultFolder(olkFolderInbox).Folders("new_Messages")
    
    Set myItems = sourceFolder.items

    str = "[senderemailaddress] = " & _
          quote("someone@somewhere.com")

    Set myRestrict = myItems.Restrict(str)
    For itemCount = myRestrict.Count To 1 Step -1
        myRestrict.Item(itemCount).Move destFolder
    Next

End Sub

Function quote(str) As String

    quote = "'" & str & "'"

End Function

Open in new window


Chris
0
 

Author Comment

by:alam747
ID: 39595781
Actually, I want to move the attachment message come in as email attachement. every email has the attachment which is a email message, wants to move those attachment email to different email folder. Not the message as it come in.

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39596273
every email from a specific addressee has the attachment (only 1?) and its always a mail in itself and it is this attachment that is to be saved?  Is the attachment to be deleted from the original email?

Also noting the zone is still excel rather than outlook do you want to trigger this activity on demand in excel/access or when the mail is received in outlook itself?

Chris
0
 

Author Comment

by:alam747
ID: 39596809
Yes, every email has an attachment, which is a email message therefore want to move the attachment of each email to a specific email folder.

Please advise the VBA code for the purposes.

Thanks
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39597648
Sub q2()
Dim olkApp As Object
Dim sourceFolder As Object
Dim destFolder As Object
Dim myItems As Object
Dim itm As Object
Dim str As String
Dim myRestrict As Object
Dim itemCount As Integer
Const olkFolderInbox As Integer = 6
Dim mai As Object
Dim nu As Object
Dim strFileName As String

    Set olkApp = CreateObject("outlook.application")
    Set sourceFolder = olkApp.Session.GetDefaultFolder(olkFolderInbox)
    Set destFolder = olkApp.Session.GetDefaultFolder(olkFolderInbox).Folders("new_Messages")
    
    Set myItems = sourceFolder.Items

    str = "[senderemailaddress] = " & _
          quote("someone@somewhere.com")

    Set myRestrict = myItems.Restrict(str)
    For itemCount = myRestrict.Count To 1 Step -1
        Set mai = myRestrict.Item(itemCount)
        strFileName = Environ("temp") & "\" & mai.Attachments(1).FileName
        mai.Attachments(1).SaveAsFile strFileName
        Set nu = Application.CreateItemFromTemplate(strFileName)
        nu.Move Application.ActiveInspector.CurrentItem.Parent
        Kill strFileName
    Next

End Sub

Function quote(str) As String

    quote = "'" & str & "'"

End Function

Open in new window

0
 

Author Closing Comment

by:alam747
ID: 39629899
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

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

20 Experts available now in Live!

Get 1:1 Help Now