Solved

VBA: Navigating to Public Folders

Posted on 2006-11-16
13
1,071 Views
Last Modified: 2012-06-21
I am using VBA to navigate to a public folder that is many levels deep.

Outlook 2002 on an Exchange Server.

Everything seems to work fine, except the code opens a new instance of Outlook.

Is there an alternate approach that would navigate to the folder of interest within the same instance of Outlook?

THANKS!

Code Follows:

Sub FindFolder()
  strFolderPath = "Public Folders\All Public Folders\Personal\Folder"
  ' MsgBox strFolderPath
  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
  ' Get EntryID for specified path
  strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = CreateObject("Outlook.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
  FolderEntryID = objFolder.EntryID
  FolderStoreID = objFolder.StoreID
  ' OK - Now open specified folder, maximize Outlook, and select FolderList view option
  Set myFolder = objNS.GetFolderFromID(FolderEntryID, FolderStoreID)
  myFolder.Display
  Application.ActiveExplorer.ShowPane Pane:=olFolderList, Visible:=True
  Application.ActiveExplorer.ShowPane Pane:=olOutlookBar, Visible:=False
  Application.ActiveExplorer.WindowState = olMaximized
End Sub
0
Comment
Question by:wkhays
  • 6
  • 5
  • 2
13 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 17959783
wkhays,

I take it you are running this from outlook? You have it coded as if you were running this from another program.

To use the same instance:
'  Set objApp = CreateObject("Outlook.Application")
  Set objApp = Application

Matt
0
 
LVL 39

Expert Comment

by:stevbe
ID: 17964377
Set objApp = GetObject(, "Outlook.Application")
If objApp Is Nothing Then
    Set objApp = CreateObject("Outlook.Application")
End If
0
 
LVL 4

Author Comment

by:wkhays
ID: 17964426

Thanks, but with both tries, a new instance is loaded.

Any other ideas?
0
 
LVL 39

Expert Comment

by:stevbe
ID: 17964647
so this is running as an Outlook vba project correct?


this is exactly what Matt posted ... and I tested it :-)
Set objApp = Application

and after readuing your code you only use the variable 1 time, you could do this instead...

 Set objNS = Application.GetNamespace("MAPI")

and drop the objApp entirely

0
 
LVL 35

Expert Comment

by:mvidas
ID: 17964687
I agree with Steve, though you could also use "Application" in place of objApp like you did in the last 3 lines of the sub.

As for the getnamespace method, you could also use:

 Set objNS = Session

To return the current session's namespace.

Matt
0
 
LVL 4

Author Comment

by:wkhays
ID: 17964768

Thanks Steve & Matt ...

Same results ... A few thoughts, though:

1)
Steve: Are you using Outlook 2002?  I understand that there were some changes in 2002 that may have caused this behavior.

2)
I am confused because according to the the Microsoft knowledgebase, all three of the following methods should return a reference to the existing instance of Outlook:

Dim olApp1 As Outlook.Application
Dim olApp2 As Outlook.Application
Dim olApp3 As Outlook.Application
'
Set olApp1 = New Outlook.Application
Set olApp2 = CreateObject("Outlook.Application")
Set olApp3 = GetObject(, "Outlook.Application")

3)
I am beginning to believe that the .display method is the problem ... And for some reason it is forcing the new instance of Outlook.

Thanks for your help ... I would certainly appreciate any more feedback you can offer!!!


0
What Is Threat Intelligence?

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

 
LVL 35

Accepted Solution

by:
mvidas earned 125 total points
ID: 17964778
After looking at it a bit closely, what about using just

Sub FindFolder()
  Dim strFolderPath As String, objFolder As MAPIFolder, arrFolders() As String, i As Long
  strFolderPath = "Public Folders\All Public Folders\Personal\Folder"
  arrFolders() = Split(Replace(strFolderPath, "/", "\"), "\")
  On Error Resume Next
  Set objFolder = Session.Folders.Item(arrFolders(0))
  If Not objFolder Is Nothing Then
    For i = 1 To UBound(arrFolders)
      Set objFolder = objFolder.Folders(arrFolders(i))
      If objFolder.Name <> arrFolders(i) Then Exit For
    Next
  End If
  On Error GoTo 0
  Set Application.ActiveExplorer.CurrentFolder = objFolder
  Application.ActiveExplorer.ShowPane Pane:=olFolderList, Visible:=True
  Application.ActiveExplorer.ShowPane Pane:=olOutlookBar, Visible:=False
  Application.ActiveExplorer.WindowState = olMaximized
End Sub

Matt
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17964806
wkhays,
Re: 3 ways of setting the reference

That is correct.  The best (IMO) is olApp2's method.  Outlook behaves differently than other Office apps, in that Create object will return the current application object if it exists, and won't if it doesn't.  Then at the end of the sub, using
Set olApp2 = Nothing
Will kill the instance if it didn't exist beforehand, and do nothing if it got the current one.

Matt
0
 
LVL 4

Author Comment

by:wkhays
ID: 17964908

Thanks to all!!

Matt ... This is a great solution.  This is my first VBA in Outlook (although I do a lot of VBA in Exel), so it was based on a lot of cutting and pasting ... In addition to giving me the functionality I needed, you cleaned up my clunky code!!!

Life is good again!
(Sad, nerdy comment ... Sorry!)
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17964935
Not a problem! I'm an excel vba guy myself, outlook has just been my new hobby the past couple months or so
You are doing the right thing by looking up different codes and trying them out, getting a feel for the object model.  If you're unaware of outlookcode.com and slipstick.com, check them out! Great resources

After looking a little more AGAIN at my sub there, change:
  On Error GoTo 0
  Set Application.ActiveExplorer.CurrentFolder = objFolder
  Application.ActiveExplorer.ShowPane Pane:=olFolderList, Visible:=True
  Application.ActiveExplorer.ShowPane Pane:=olOutlookBar, Visible:=False
  Application.ActiveExplorer.WindowState = olMaximized

To:

  On Error GoTo 0
  If Not objFolder Is Nothing Then
    Set Application.ActiveExplorer.CurrentFolder = objFolder
    Application.ActiveExplorer.ShowPane Pane:=olFolderList, Visible:=True
    Application.ActiveExplorer.ShowPane Pane:=olOutlookBar, Visible:=False
    Application.ActiveExplorer.WindowState = olMaximized
  End If

Can't be too careful :)
0
 
LVL 4

Author Comment

by:wkhays
ID: 17965022

OK ... You did that in case the folder did not exist ... An alternate would be to not disable the error handling command ... right?

BTW: have you come across an Auto_Open() macro name that executes when Outlook is first opened?
0
 
LVL 35

Expert Comment

by:mvidas
ID: 17965100
That is right, though if you didnt put the On Error statements and your public folder path was incorrect, it would error out. I like the way you have it written, in that it will go to the last existing folder in your path.  So if \public folders\a\b\c existed, and you used \public folder\a\b\c\d\e, it would still go to 'c' as that exists.

Re: BTW, yes :)
Use the "ThisOutlookSession" class object in the vbaproject.otm file, there are a few events (you can use the dropdowns to see them).  The one you're looking for is Application_Startup
And feel free to keep asking questions in this thread, it won't lock for 7 days and I'm happy to help
0
 
LVL 4

Author Comment

by:wkhays
ID: 17966550

Awesome ... THANKS!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
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…

708 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