Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Outlook Free & Paid Tools
If you don't know how to downgrade, my instructions below should be helpful.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
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: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now