Solved

To move email message to another email folder

Posted on 2013-10-22
8
331 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

828 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