Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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