Solved

Outlook Favorite Folders - add programatically?

Posted on 2009-04-02
13
2,452 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
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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now