[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2865
  • Last Modified:

Outlook Favorite Folders - add programatically?

Does anyone know a way of adding folders to Outlook's Favorite Folder list using code/batch/script files?
0
cjohnson300
Asked:
cjohnson300
1 Solution
 
David LeeCommented:
Hi, cjohnson300.  

If you have Outlook 2007, then this will work.  Not aware of any way to do this via code with older versions of Outlook.  
Sub AddFavoriteFolder(olkFolder As Outlook.Folder)
    Dim olkPane As Object, _
        olkModule As Object, _
        olkGroup As Object
    Set olkPane = Outlook.Application.ActiveExplorer.NavigationPane
    Set olkModule = olkPane.Modules.GetNavigationModule(olModuleMail)
    Set olkGroup = olkModule.NavigationGroups.GetDefaultNavigationGroup(olFavoriteFoldersGroup)
    olkGroup.NavigationFolders.Add olkFolder
    Set olkPane = Nothing
    Set olkModule = Nothing
    Set olkGroup = Nothing
End Sub

Open in new window

0
 
cjohnson300Author Commented:
Thanks for that, does that need to be called from within Outlook??  Or can be put into VBScript??
0
 
David LeeCommented:
There was not mention of VBScript, so this version is in VBA and would need to run from inside Outlook.  Does it need to be in VBScript?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cjohnson300Author Commented:
I would have preferred something independent

Sorry ;-(
0
 
David LeeCommented:
I can convert it to VBScript.  Not a big deal.
0
 
cjohnson300Author Commented:
thanks!
0
 
David LeeCommented:
Ok, here's the VBScript version of this.  Pay attention to the comments in the file.
'Define some constants'
Const olMailModule = 158
Const olFavoriteFoldersGroup = 4
 
'Create some variables'
Dim olkApp, olkSes, olkPane, olkModule, olkGroup, olkFolder
 
'Connect to Outlook'
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon "Outlook"
 
'Get the Favorites group'
Set olkPane = olkApp.ActiveExplorer.NavigationPane
Set olkModule = olkPane.Modules.GetNavigationModule(olModuleMail)
Set olkGroup = olkModule.NavigationGroups.GetDefaultNavigationGroup(olFavoriteFoldersGroup)
 
'Get the folder to add'
Set olkFolder = OpenOutlookFolder("Projects\Domain Consolidation")    '<- change the folder path here'
 
'Add the folder to favorites'
olkGroup.NavigationFolders.Add olkFolder
 
'Clean-up'
Set olkPane = Nothing
Set olkModule = Nothing
Set olkGroup = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit
 
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
 
Function OpenOutlookFolder(strFolderPath)
    Dim arrFolders, varFolder, olkMyFolder
    Set OpenOutlookFolder = Nothing
    Set olkMyFolder = Nothing
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkMyFolder) Then
                Set olkMyFolder = olkSes.Folders(varFolder)
            Else
                Set olkMyFolder = olkMyFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkMyFolder
    End If
    On Error Goto 0
End Function

Open in new window

0
 
cjohnson300Author Commented:
Getting the following message, in what form does the olkFolder variable need to be in when its a subfolder of my inbox or a public folder or a folder within my Mailbox??

Untitled.jpg
0
 
David LeeCommented:
The path to a folder in Outlook is exactly like the path to a folder in the file system with the exception that Outlook folder paths do not use drive letters.  Consider the following folder structure

+ Mailbox - Doe, John
   + Inbox
      - Folder A
+ Public Folders
   + All Public Folders
      - Folder B

The path to Folder A is

   Mailbox - Doe, John\Inbox\Folder A

The path to Folder B is

    Public Folders\All Public Folders\Folder B

Line 19 must contain a valid folder path.
0
 
cjohnson300Author Commented:
That's great!  Only problem is that in order to add a Public Folder to your Favorites, you need to add the Public Folder to your Public Folder Favorites first.  Is there a couple of lines of code to cope with this?
0
 
David LeeCommented:
Try this version.
'Define some constants'
Const olMailModule = 158
Const olFavoriteFoldersGroup = 4
 
'Create some variables'
Dim olkApp, olkSes, olkPane, olkModule, olkGroup, olkFolder
 
'Connect to Outlook'
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon "Outlook"
 
'Get the Favorites group'
Set olkPane = olkApp.ActiveExplorer.NavigationPane
Set olkModule = olkPane.Modules.GetNavigationModule(olModuleMail)
Set olkGroup = olkModule.NavigationGroups.GetDefaultNavigationGroup(olFavoriteFoldersGroup)
 
'Get the folder to add'
Set olkFolder = OpenOutlookFolder("Projects\Domain Consolidation")    '<- change the folder path here'
'If this is a public folder, then add it to public folder favorites'
If InStr(1, olkFolder.FolderPath, "Public Folders") Then
    olkFolder.AddToPFFavorites
End If
 
'Add the folder to favorites'
olkGroup.NavigationFolders.Add olkFolder
 
'Clean-up'
Set olkPane = Nothing
Set olkModule = Nothing
Set olkGroup = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit
 
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
 
Function OpenOutlookFolder(strFolderPath)
    Dim arrFolders, varFolder, olkMyFolder
    Set OpenOutlookFolder = Nothing
    Set olkMyFolder = Nothing
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkMyFolder) Then
                Set olkMyFolder = olkSes.Folders(varFolder)
            Else
                Set olkMyFolder = olkMyFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkMyFolder
    End If
    On Error Goto 0
End Function

Open in new window

0
 
gcrichtonCommented:
Thanks a mill "ID:24048798Author:BlueDevilFan"

I was able to modify and use this to add CRM favories to about 100 users, you rock!!

Very nicely marked and simple to use!

Cheers.
0
 
OTSSolutionsCommented:
Hello BlueDevilFan,

I need to show public folder calender to mail favorite  folder.  Is there any way to do it via login script or any policy.
 I hope to listen from your end.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now