?
Solved

Macro syntax to add folder to outlook

Posted on 2014-03-20
3
Medium Priority
?
506 Views
Last Modified: 2014-03-21
I am using a macro to move my messages older than a day to specific folders. I have included the code I am using below.

Is there some code that could be added that will create a folder/folder structure if it doesn't exist?

Here is my code:

Sub MoveAgedMail()

'adjust mailboxNameString to match your name - it will be the way it displays in outlook
    mailboxNameString = "email@test.com"

'Check to make sure variable stored string correctly.  Opportunity to kill script before it starts.
    MsgBox mailboxNameString
    
'Define outlook objects:
    Dim objOutlook As Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim obDestFolder As Outlook.MAPIFolder
    Dim objVariant As Variant

'Define Counters

    'Used for loop to comb through Inbox folder
    Dim intCount As Integer

    'Different ways to do with logic I did it quick and messy without it will make a copy in original folder and then also another folder that you set for all older mail if you setup similarly.
    Dim moveOnce As Integer
             
    ' Set variable values
    Set objOutlook = Application
    Set objNameSpace = objOutlook.GetNamespace("MAPI")
    Set objSourceFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
             
    'Start loop to loop through each item in the inbox
    For intCount = objSourceFolder.Items.Count To 1 Step -1
    'This defines the variable as the mail item you are currently on in the loop which you can then manipulate, move or apply logic to.
        Set objVariant = objSourceFolder.Items.Item(intCount)

        DoEvents
        'Set move counter to 0 so it will reset upon every loop, it will add 1 if its processed by the logic and at the end if it had been passed by the custom folder rules it will move to the catch Older than a Day folder.
        'NOTE: Older than a day folder is completely optional you can leave out and items that dont match any othe folder / sender criteria will just remain in inbox.  Completely up to you.
        moveOnce = 0
        
        'Used for testing - Used to make sure that the variable was resetting.
        'MsgBox "Move Once = " & moveOnce
        
        'If object is a mail item Then...
        If objVariant.Class = olMail Then
            'Perform logic to determine difference between NOW and the date of the item.
            intDateDiff = DateDiff("d", objVariant.SentOn, Now)
            'If Date is older than 0 days (or today) then apply the following logic
            If intDateDiff > 0 Then
                'Define Sender of item
                strSender = objVariant.SenderName
               
                'IF Certain Sender then send to a folder
                'NOTE: Please check script description for a logical example to follow along with

'Move items older than a day to standalone folder to keep e-mails organized

'Example 1
                If strSender = "Voicemail Sender" Then
                    Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Voicemail")
                    objVariant.Move obDestFolder
               'Add move counter so at the end item wont be moved to two locations
                    moveOnce = 1
                End If

            End If
        End If
    
    Next
    Exit Sub
End Sub

Open in new window

0
Comment
Question by:Zac Harris
[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
3 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 2000 total points
ID: 39943897
You'd do something like

                If strSender = "Voicemail Sender" Then
                    'Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Voicemail")

                     Set obDestFolder = objNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders.Add("Voicemail_2")

                    objVariant.Move obDestFolder
               'Add move counter so at the end item wont be moved to two locations
                    moveOnce = 1
                End If

OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 39943907
See this article for more information  on Folders.Add method
http://msdn.microsoft.com/en-us/library/office/ff862204(v=office.15).aspx

OM Gang
0
 
LVL 14

Author Closing Comment

by:Zac Harris
ID: 39945092
Works perfectly!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
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…
Suggested Courses

771 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