[Webinar] Streamline your web hosting managementRegister Today

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

VBA: Navigating to Public Folders

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
wkhays
Asked:
wkhays
  • 6
  • 5
  • 2
1 Solution
 
mvidasCommented:
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
 
stevbeCommented:
Set objApp = GetObject(, "Outlook.Application")
If objApp Is Nothing Then
    Set objApp = CreateObject("Outlook.Application")
End If
0
 
wkhaysAuthor Commented:

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

Any other ideas?
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
stevbeCommented:
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
 
mvidasCommented:
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
 
wkhaysAuthor Commented:

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
 
mvidasCommented:
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
 
mvidasCommented:
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
 
wkhaysAuthor Commented:

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
 
mvidasCommented:
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
 
wkhaysAuthor Commented:

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
 
mvidasCommented:
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
 
wkhaysAuthor Commented:

Awesome ... THANKS!
0

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now