?
Solved

Outlook Favorite Folders - add programatically?

Posted on 2009-04-02
13
Medium Priority
?
2,705 Views
Last Modified: 2012-05-06
Does anyone know a way of adding folders to Outlook's Favorite Folder list using code/batch/script files?
0
Comment
Question by:cjohnson300
[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
13 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 24047944
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
 

Author Comment

by:cjohnson300
ID: 24048020
Thanks for that, does that need to be called from within Outlook??  Or can be put into VBScript??
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24048040
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:cjohnson300
ID: 24048250
I would have preferred something independent

Sorry ;-(
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24048371
I can convert it to VBScript.  Not a big deal.
0
 

Author Comment

by:cjohnson300
ID: 24048387
thanks!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24048798
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
 

Author Comment

by:cjohnson300
ID: 24048960
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
 
LVL 76

Expert Comment

by:David Lee
ID: 24049100
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
 

Author Comment

by:cjohnson300
ID: 24049584
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
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 24054818
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
 

Expert Comment

by:gcrichton
ID: 26553770
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
 

Expert Comment

by:OTSSolutions
ID: 37060396
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
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

764 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