Solved

To move email message to another email folder

Posted on 2013-10-22
8
323 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
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…
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 …
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.

760 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

23 Experts available now in Live!

Get 1:1 Help Now