Consulting

Results 1 to 7 of 7

Thread: Jump to specific folder in a shared mailbox

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Posts
    6
    Location

    Jump to specific folder in a shared mailbox

    Hello wise ones

    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.

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    VBAX Regular
    Joined
    Feb 2017
    Posts
    6
    Location
    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
    Last edited by CoNS; 02-22-2017 at 12:49 PM.

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  5. #5
    VBAX Regular
    Joined
    Feb 2017
    Posts
    6
    Location
    Thank you so much! Everything works like a charm now. Your help is much appreciated.

    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)

  6. #6
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  7. #7
    VBAX Regular
    Joined
    Feb 2017
    Posts
    6
    Location
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •