Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

To move email message to another email folder

Posted on 2013-10-22
8
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 70

Expert Comment

by:KCTS
ID: 39594635
0
 
LVL 101

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

715 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