Solved

To move email message to another email folder

Posted on 2013-10-22
8
336 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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 …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

737 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