PDA

View Full Version : [SOLVED:] Jump to specific folder in a shared mailbox



CoNS
02-22-2017, 07:30 AM
Hello wise ones :content:

I am rather new to VBA / macro programming. I have spent most of the day today trying to put together a macro, which will open a folder in Outlook 2016's explorer view (folder pane). The folder is a specific subfolder of a subfolder in the inbox of a shared mailbox. I would like the macro to work no matter if I currently have selected an email in a different mailbox (my personal mailbox).

I have looked at various code snippets from different websites, but I have not yet managed to hit something which works - although I have a feeling that the solution is probably quite simple and would only require a few lines of code.

Can anyone of you help, please? Thanks in advance. :help

skatonni
02-22-2017, 10:31 AM
This works for any mailbox. Not limited by olFolderInbox which is for the default mailbox only.


Option Explicit

Sub Folder_AnyMailbox()

Dim strMailboxName As String
Dim myfolder As Folder

strMailboxName = "Name of mailbox"

Set myfolder = Session.Folders(strMailboxName)
Set myfolder = myfolder.Folders("Inbox")
Set myfolder = myfolder.Folders("Name of Subfolder 1 under Inbox")
Set myfolder = myfolder.Folders("Name of Subfolder 2 under Subfolder 1")

myfolder.Display

ExitRoutine:
Set myfolder = Nothing

End Sub

CoNS
02-22-2017, 12:10 PM
Thanks a lot. It works :-)

Subfolder 2 has a number of further subfolders underneath it. When I run the macro, Subfolder 2 is selected as it should, but it is collapsed... How can I get the selected Subfolder 2 to be expanded, too, when the macro is run?

EDIT: One more thing... When I run the macro suggested in the above post, another instance of Outlook 2016 is opened. I would like to avoid this, and have the relevant subfolder selected and displayed in the original instance of Outlook 2016. Similar to this other macro:


Public Sub GetItemsFolderPath()
Dim obj As Object
Dim F As Outlook.MAPIFolder
Dim Msg$
Set obj = Application.ActiveWindow
If TypeOf obj Is Outlook.Inspector Then
Set obj = obj.CurrentItem
Else
Set obj = obj.Selection(1)
End If
Set F = obj.Parent
Msg = "The path is: " & F.FolderPath & vbCrLf
Msg = Msg & "Switch to the folder?"
If MsgBox(Msg, vbYesNo) = vbYes Then
Set Application.ActiveExplorer.CurrentFolder = F
End If
End Sub

skatonni
02-22-2017, 02:53 PM
You see the folder at this line


Set Application.ActiveExplorer.CurrentFolder = F


The updated Folder_AnyMailbox:



Option Explicit

Sub Folder_AnyMailbox()

Dim strMailboxName As String
Dim myfolder As Folder

strMailboxName = "Name of mailbox"


Set myfolder = Session.Folders(strMailboxName)
Set myfolder = myfolder.Folders("Inbox")
Set myfolder = myfolder.Folders("Subfolder 1")
Set myfolder = myfolder.Folders("Subfolder 2")

'myfolder.Display
Set ActiveExplorer.CurrentFolder = myfolder

If myfolder.Folders.Count > 0 Then

' Open the first subfolder of Subfolder 2
Set ActiveExplorer.CurrentFolder = myfolder.Folders(1)

' Open Subfolder 2
Set ActiveExplorer.CurrentFolder = myfolder

End If

ExitRoutine:
Set myfolder = Nothing

End Sub

CoNS
02-23-2017, 01:16 AM
Thank you so much! Everything works like a charm now. Your help is much appreciated. :bow:

One last question out of curiosity, before I mark the thread with SOLVED: What does the ExitRoutine bit at the end do? Is it just proper housecleaning or does it serve another purpose? Thanks again. (I have some further questions in relation to the other GetItemsFolderPath macro I posted above, but since that would be different topic I will create a separate thread for that shortly)

skatonni
02-24-2017, 10:26 AM
For housekeeping, free memory with = Nothing. Others have stated it mattered in certain situations they encountered. I do this by habit since I have yet to see a problem when I forgot to clean up.

CoNS
02-25-2017, 01:57 AM
Understood. Thanks again. I will add SOLVED to the title of the thread. Might you be able to also help me with tweaking the GetItemsFolderPath macro further, as per my separate new thread? :dau: