Solved

Outlook Favorite Folders - add programatically?

Posted on 2009-04-02
13
2,647 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Changing a few Outlook Options can help keep you organized!
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…
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…

688 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