OEMUSKZLXI ?
Subscribe to RSS feed

Home :.: About :.: Contact :.: Archives :.: Bookmarks :.: WishList


I have setup my Microsoft Outlook to include the gmail IMAP box (using these settings):

Incoming Mail (IMAP) Server – requires SSL: imap.gmail.com
Use SSL: Yes
Port: 993
Outgoing Mail (SMTP) Server – requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587
Account Name: your full email address (including @gmail.com) Google Apps users, please enter username@your_domain.com
Email Address: your full Gmail email address (username@gmail.com) Google Apps users, please enter username@your_domain.com
Password: your Gmail password

However, I noted that sometimes the rules that I setup in Microsoft Outlook didn’t fire. Via the Outlook rules interface it is not possible to run all rules, you have to checkmark each single one that you want to run, in my case, with the zillions of rules (moving all regular “newsletter” mails to its corresponding fire and forget boxes) that was no option.

So with the help of this site I included the following code in my macro editor (first 2 functions are not needed but are helpful to find out the correct names of the store and folderpaths)

Sub EnumerateFoldersInStores()
    Dim olApp As New Outlook.Application
    Dim colStores As Outlook.Stores
    Dim oStore As Outlook.Store
    Dim oRoot As Outlook.Folder
    
    On Error Resume Next
    Set colStores = olApp.Session.Stores
    For Each oStore In colStores
        Debug.Print (oStore.DisplayName)
               
        Set oRoot = oStore.GetRootFolder
        Debug.Print (oRoot.FolderPath)
        EnumerateFolders oRoot
    Next
End Sub
 
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
    Dim folders As Outlook.folders
    Dim Folder As Outlook.Folder
    Dim foldercount As Integer
    
    On Error Resume Next
    Set folders = oFolder.folders
    foldercount = folders.count
    'Check if there are any folders below oFolder
    If foldercount Then
        For Each Folder In folders
            Debug.Print (Folder.FolderPath)
            EnumerateFolders Folder
        Next
    End If
End Sub
 
Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' strFolderPath needs to be something like
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"
 
  Dim objApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim colFolders As Outlook.folders
  Dim objFolder As Outlook.MAPIFolder
  Dim arrFolders() As String
  Dim I As Long
  On Error Resume Next
 
  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = Application
  Set objNS = objApp.GetNamespace("MAPI")
  Set objFolder = objNS.folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For I = 1 To UBound(arrFolders)
      Set colFolders = objFolder.folders
      Set objFolder = Nothing
      Set objFolder = colFolders.Item(arrFolders(I))
      If objFolder Is Nothing Then
        Exit For
      End If
    Next
  End If
 
  Set GetFolder = objFolder
  Set colFolders = Nothing
  Set objNS = Nothing
  Set objApp = Nothing
End Function
 
Sub RunAllInboxRules()
    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim count As Integer
    Dim ruleList As String
    'On Error Resume Next
        
    ' get default store (where rules live)
    Set st = Application.Session.Stores.Item("MYNAME@gmail.com")
    ' get rules
    Set myRules = st.GetRules
    
    ' Set myfolder = GetFolder("Personal Folders/Inbox")
    Set myfolder = GetFolder("MYNAME@gmail.com\Inbox")
        
    ' iterate all the rules
    For Each rl In myRules
        ' determine if it's an Inbox rule
        If rl.RuleType = olRuleReceive Then
            ' if so, run it
            rl.Execute ShowProgress:=True
            rl.Execute IncludeSubfolders:=True
                       
            rl.Execute Folder = myfolder
            
            count = count + 1
            ruleList = ruleList & vbCrLf & rl.Name
        End If
    Next
    
    ' tell the user what you did
    ruleList = "These rules were executed against the Inbox: " & myfoldername & vbCrLf & ruleList
    MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules"
    
    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
End Sub
 
 

And then added the button to my outlook bar:

image

Works like a charm. This is the first time I did anything in Outlook coding. But it seemed interesting enough to dive into somewhat further.

Related posts

del.ico.us Del.icio.us

digg Digg

ekus Ekudos

reddit reddit

 coding

 gmail

 outlook

November 15th, 2008