Solved

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

Posted on 2013-01-31
4
781 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

RoboForm Secure Password Management System

RoboForm Everywhere - Superb Browser Support
Windows / Apple / IOS / Android / Linux / Chrome OS
Use different complex passwords everywhere
Best Secure Password Management by far
Synchronize all of your devices instantly
Safe, Secure & Highly Recommended!

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

751 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