Solved

Outlook 2010 VBA not working to move Email to a different folder

Posted on 2013-01-31
4
777 Views
Last Modified: 2013-01-31
When certain emails arrive, I want to set their category and move them to a specific folder. The following code sets the category successfully, but does not move it to the folder.

(FWIW: The "Alerts" folder is not a subfolder - it is a peer with the InBox)

What am I missing?


Public Sub MoveToFolder(MyEmail As Outlook.MailItem)
    
    Dim MyNamespace As Outlook.NameSpace
    Dim DestFolder As Outlook.MAPIFolder
    
    Set MyOutlook = Outlook.Application
    Set MyNamespace = MyOutlook.GetNamespace("MAPI")

    Set DestFolder = MyNamespace.Folders("Firstname.Lastname@website.com").Folders("Alerts")
    
    MyEmail.Categories = "Problem Report"
    MyEmail.Save
    
    MyEmail.Move (DestFolder)
    MyEmail.Save
        
End Sub

Open in new window

0
Comment
Question by:dossbob
[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
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 38841111
The brackets on the destfolder for the move statement are not necessary but I doubt thats it,
put the cursor on the alerts folder in the outlook application explorer then type this in the VBE immediate window (ctrl + G to display):

?application.ActiveExplorer.CurrentFolder.FolderPath

The resultant becomes:

MyNamespace.Folders(PST Name).Folders("Alerts")

Add as many .folders as are necessary to populate all the components in the returned folder address.  ANy problems then get back with the info.

Chris
0
 

Author Comment

by:dossbob
ID: 38841680
1. If the "outlook application explorer" is the window in the bottom left corner, it doesn't have anything at all in it. (Maybe I just don't know how to get it populated)

2. FWIW: Since I don't have an "Alerts" folder in the application explorer, I tried just typing the command you suggested, and got "\\Firstname.Lastname@website.com\Inbox", which is what I would expect.

3. I put a debug message between every line, so I know for sure that all lines execute EXCEPT the last two lines, i.e. the script is failing on the "MyEmail.Move (DestFolder)" line.
0
 

Author Comment

by:dossbob
ID: 38841717
I just re-read your post, and tried removing the parentheses around DestFolder.

Strange as it seems that fixed the problem.

<gripe on>
I've been plagued by punctuation marks since Fortran II on punch cards in 1968. It just seems like compilers would have gotten better in 45 years (:<)
<gripe off>
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38842538
I am somewhat surprised, I wasn't in a position to try the comment re brackets but would expect the compiler to throw an error hence my thought that it was unlikely.  That said i'm glad it helped.

Chris
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Find out what you should include to make the best professional email signature for your organization.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

756 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