How to find the projectfolder of an email

Teunis
3StarLounger
Posts: 218
Joined: 02 Feb 2010, 23:10

How to find the projectfolder of an email

Post by Teunis »

In Outlook (2010), I have a number of projectfolders, each one having a "sent" and "received" subfolder. When I search for a certain email, it gives me the email and the subfolder it resides, which is "sent" or "received" (or deleted etc). My question is, how to find the associated project folder one level above the "sent" and "received"?

Regards, Teunis

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

Teunis wrote:...My question is, how to find the associated project folder one level above the "sent" and "received"?
If you find an answer outside this forum then please post the solution here, as I have frequently needed to do this too.
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

I would include the project name in the name of the folders, e.g. if you have a folder "Benin 2011", I'd name the subfolders "Benin 2011 Received" and "Benin 2011 Sent".

But here is a macro you can run to get the "parent" folder of the folder containing the e-mail:

Code: Select all

Sub GetParentFolder()
  Dim strMsg As String

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Inspector" Then
    strMsg = ActiveInspector.CurrentItem.Parent.Parent
  ElseIf TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.Selection.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        strMsg = ActiveExplorer.Selection.Item(1).Parent.Parent
      Else
        strMsg = "You have selected more than one item."
      End If
    Else
      strMsg = "You haven't selected any items."
    End If
  End If

  MsgBox strMsg, vbExclamation
  Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation
End Sub
It appears to work correctly but I haven't really stress-tested it.
Best wishes,
Hans

Teunis
3StarLounger
Posts: 218
Joined: 02 Feb 2010, 23:10

Re: How to find the projectfolder of an email

Post by Teunis »

Thanks Hans,
Happy New Year to you!
Of course, naming the subfolders more meaningfull is a logical solution. I prefer your macro though, but please elaborate a bit how to apply it.

Regards, Teunis

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

HansV wrote:...
It appears to work correctly but I haven't really stress-tested it.
This works fine for me on Outlook 2010 (14.0.4760.1000). I tested it with an item stored in an exchange cached folder and an item in a local PST file.

If you have some time and it is not too difficult, it would be really helpful if you could provide a version of this that recurses up the folders until it got to the top, as my problem is usually trying to identify an item that I have found by searching and it may be many levels down.
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

Happy New Year!

Press Alt+F11 to activate the Visual Basic Editor.
Select Insert | Module.
Paste the code from my previous reply into the module.

Switch back to Outlook.
Do a search, and select one of the found items or open it (the code doesn't only work with search results, by the way, but with any item).
Press Alt+F8, select GetParentFolder and click Run.

To make the macro easier to use, you can assign it to a custom toolbar button (Outlook 2007 and before), or to a Quick Access Toolbar button (Outlook 2010).

Note: when you quit Outlook, it'll ask whether you want to save changes to the VBA project. Answer Yes, otherwise the code will be lost.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

StuartR wrote:If you have some time and it is not too difficult, it would be really helpful if you could provide a version of this that recurses up the folders until it got to the top, as my problem is usually trying to identify an item that I have found by searching and it may be many levels down.
Stuart,

Does this do what you want?

Code: Select all

Sub GetParentFolder()
  Dim strMsg As String
  Dim fld As Object

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Inspector" Then
    strMsg = ActiveInspector.CurrentItem.Parent.Parent
  ElseIf TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.Selection.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        Set fld = ActiveExplorer.Selection.Item(1).Parent
        On Error Resume Next
        Do
          strMsg = "\" & fld.Name & strMsg
          Set fld = fld.Parent
        Loop Until Err
        strMsg = "Path: " & Mid(strMsg, 2)
      Else
        strMsg = "You have selected more than one item."
      End If
    Else
      strMsg = "You haven't selected any items."
    End If
  End If

  MsgBox strMsg, vbInformation
  Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation
End Sub
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

HansV wrote:...Does this do what you want?
...
Nearly.

If I select a folder and run this Macro then it displays the full path to the folder.
If I open an item and run the Macro then it just displays the name of the parent folder.
If I run advanced search and select the file in the search window then the Macro reports "The operation failed".
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

Sorry, I forgot to modify the part that handles an opened item. Currently, I don't know how to work with items in the Advanced Find results pane, I'll look into that.

In the meantime, here is a version that works from an open item too:

Code: Select all

Sub GetParentFolder()
  Dim strMsg As String
  Dim fld As Object

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Inspector" Then
    Set fld = ActiveInspector.CurrentItem.Parent
    On Error Resume Next
    Do
      strMsg = "\" & fld.Name & strMsg
      Set fld = fld.Parent
    Loop Until Err
    strMsg = "Path: " & Mid(strMsg, 2)
  ElseIf TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.Selection.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        Set fld = ActiveExplorer.Selection.Item(1).Parent
        On Error Resume Next
        Do
          strMsg = "\" & fld.Name & strMsg
          Set fld = fld.Parent
        Loop Until Err
        strMsg = "Path: " & Mid(strMsg, 2)
      Else
        strMsg = "You have selected more than one item."
      End If
    Else
      strMsg = "You haven't selected any items."
    End If
  End If

  MsgBox strMsg, vbInformation
  Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation
End Sub
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

I'm afraid that the Advanced Find dialog is not exposed to VBA (Outlook MVP Sue Mosher says so, and she knows Outlook VBA inside and out), so there is no way to work with items in the results pane. I realize that's a serious restriction of the usefulness of the code, but the only workaround would be to create a userform that mimics the functionality of the Advanced Find dialog, and that's outside the scope of this thread.

This version displays a slightly more informative error message:

Code: Select all

Sub GetParentFolder()
  Dim strMsg As String
  Dim fld As Object

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Inspector" Then
    Set fld = ActiveInspector.CurrentItem.Parent
    On Error Resume Next
    Do
      strMsg = "\" & fld.Name & strMsg
      Set fld = fld.Parent
    Loop Until Err
    strMsg = "Path: " & Mid(strMsg, 2)
  ElseIf TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.Selection.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        Set fld = ActiveExplorer.Selection.Item(1).Parent
        On Error Resume Next
        Do
          strMsg = "\" & fld.Name & strMsg
          Set fld = fld.Parent
        Loop Until Err
        strMsg = "Path: " & Mid(strMsg, 2)
      Else
        strMsg = "You have selected more than one item."
      End If
    Else
      strMsg = "You haven't selected any items."
    End If
  End If

ExitHandler:
  MsgBox strMsg, vbInformation
  Exit Sub

ErrHandler:
  If Err = 430 Then
    strMsg = "Sorry, the macro doesn't work with Advanced Find."
  Else
    strMsg = Err.Description
  End If
  Resume ExitHandler
End Sub
Added later: In Outlook 2007 or later, one can use the FolderPath property instead of recursing the parent folders, but that would make the above code incompatible with earlier versions.
Best wishes,
Hans

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: How to find the projectfolder of an email

Post by Goshute »

Hans, I've just tested in Outlook 2002 and 2003, and can confirm that FolderPath is available from Outlook 2002 up.

If Cbool(Inspectors.Count) Then
Debug.Print ActiveInspector.Parent.CurrentItem.FolderPath
End If

Don't know about Outlook 2000.
Goshute
I float in liquid gardens

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

This is now exactly what I need, thank you so much.

In Outlook 2010 the Err number is -2147467262, not 430, but I have corrected that myself.

Code: Select all

    Select Case Err
        Case 430, -2147467262
            strMsg = "Sorry, the macro doesn't work with Advanced Find."
        Case Else
            strMsg = Err.Description
    End Select
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

John, thanks for checking! The Outlook VBA help in Outlook 2007 says "Version Added: Outlook 2007" but I think I see why that's misleading - in Outlook 2007, the MAPIFolder object was replaced with the Folder object.

Stuart, thanks for the error number!

So here is a shortened version that should work in Outlook 2002 and later (FolderPath did not exist yet in Outlook 2000):

Code: Select all

Sub GetParentFolder()
  Dim strMsg As String

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Inspector" Then
    strMsg = "Path: " & ActiveInspector.CurrentItem.Parent.FolderPath
  ElseIf TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.Selection.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        strMsg = "Path: " & ActiveExplorer.Selection.Item(1).Parent.FolderPath
      Else
        strMsg = "You have selected more than one item."
      End If
    Else
      strMsg = "You haven't selected any items."
    End If
  End If

ExitHandler:
  MsgBox strMsg, vbInformation
  Exit Sub

ErrHandler:
  Select Case Err
    Case 430, -2147467262
      strMsg = "Sorry, the macro doesn't work with Advanced Find."
    Case Else
      strMsg = Err.Description
  End Select
  Resume ExitHandler
End Sub
Users of Outlook 2000 should be able to use the version from my previous reply.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

Thanks for that. One small difference between the two versions is how they handle a / character in a folder name. Your original version displayed this correctly, the new one replaces / with %2F This is not a significant issue, but I thought you might find it interesting.
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

That must be a peculiarity of the FolderPath property. I could correct for that, but then you might as well use the previous version.
Best wishes,
Hans

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: How to find the projectfolder of an email

Post by Goshute »

That's probably because forward and back slashes are legal in a folder name, so if one just wants to see the entire tree in a string, use FolderPath, but if one actually wants to walk through the tree, a loop is necessary...

EDIT: a loop is necessary because the output of FolderPath uses "/" as the folder separator, but a folder name can also contain "/", so one can't simply take the return from FolderPath and parse it into Folders on "/" using Split or whatever.
Last edited by Goshute on 04 Jan 2011, 18:03, edited 1 time in total.
Goshute
I float in liquid gardens

Teunis
3StarLounger
Posts: 218
Joined: 02 Feb 2010, 23:10

Re: How to find the projectfolder of an email

Post by Teunis »

StuartR wrote:
Teunis wrote:...My question is, how to find the associated project folder one level above the "sent" and "received"?
If you find an answer outside this forum then please post the solution here, as I have frequently needed to do this too.
Stuart,

I posted this question also on the Windows Secrets board. Please see John FB's reply to my post.

Regards, Teunis

Teunis
3StarLounger
Posts: 218
Joined: 02 Feb 2010, 23:10

Re: How to find the projectfolder of an email

Post by Teunis »

HansV wrote:Happy New Year!

Press Alt+F11 to activate the Visual Basic Editor.
Select Insert | Module.
Paste the code from my previous reply into the module.
....
Thanks Hans (and the others). I took the last published version and it works for me. See my answer to Stuart R for a possible alternative I received from John BF in Windows Secrets.

Regards, Teunis

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to find the projectfolder of an email

Post by StuartR »

HansV wrote:...So here is a shortened version that should work in Outlook 2002 and later (FolderPath did not exist yet in Outlook 2000)...
I was astonished at how well this worked when I needed it this morning.

I have a number of SharePoint sites synchronised with Outlook, and there is a default search folder that shows me unread items in SharePoint. this morning I saw a whole folder full of documents that were unread, but I could not tell which SharePoint they were on. One click of the button in the Quick Access Toolbar and the full path was revealed.

Great stuff.

Thank you
StuartR


User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to find the projectfolder of an email

Post by HansV »

Nice to hear it's useful! :smile:
Best wishes,
Hans