Outlook: Mark As Read VB Code

I found the following question, which answered my question concerning automatically setting an Outlook folder which I moved some emails to be marked as read automatically.


However, I am not able to put in the correct folder path for the VB code.
Portion of code which needs to be changed:

'Change the folder path on the following line as needed
    Set olkFolder1 = OpenOutlookFolder("Mailbox - Doe, John\MyFolderName")

Please advise how to determine the correct file path to use in Outlook?

Who is Participating?
David LeeConnect With a Mentor Commented:
All of the solutions I can think of require accessing the folder via its folder path.  Add this code to that you already have.  Select the target folder, then run this macro.  It'll display the folder's path.  Remove the two backslashes (if there are any) from the beginning and try using it with the original code.
Sub GetOutlookFolderPath()
    Dim olkFolder As Outlook.MAPIFolder
    Set olkFolder = Application.ActiveExplorer.CurrentFolder
    MsgBox "The path to this folder is:" & vbCrLf & vbCrLf & olkFolder.FolderPath, vbInformation + vbOKOnly, "Get Outlook Folder Path"
    Set olkFolder = Nothing
End Sub

Open in new window

David LeeCommented:
Hi, r_may.

Folder paths in Outlook are identical to folder paths in the file system, with the exception that they do not employ drive letters.  A folder path is a nothing more than a list of all the folders from the root folder to the target folder with each level being separated from the previous level by a backslash character.  Assume a folder structure like this

Mailbox - Doe, John
    + Inbox
       + Subfolder1
    + Tasks
Personal Folders
    + Projects
       + Project 1

The path to Subfolder1 is

    Mailbox - Doe, John\Inbox\Subfolder1

The path to Projects is

    Personal Folders\Projects
I use the following code and then you can do :

Item.Move GetFolder("Personal Folders\Inbox\Important\Other Systems\Fr-servers")
Public Function GetFolder(strFolderPath As String) As MAPIFolder
  ' strFolderPath needs to be something like
  '   "Public Folders\All Public Folders\Company\Sales" or
  '   "Personal Folders\Inbox\My Folder"
  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
  ' strFolderPath = Replace(strFolderPath, "/", "\")
  arrFolders() = Split(strFolderPath, "\")
  Set objApp = 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
  End If
  If objFolder Is Nothing Then
        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
  End If
  Set GetFolder = objFolder
  Set colFolders = Nothing
  Set objNS = Nothing
  Set objApp = Nothing
End Function

Open in new window

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

r_mayAuthor Commented:
Hi - Thanks for all the feedback.  I unfortunately was not able to get either solution to work.  Please find attached my modified code.  Your review would be greatly appreciated.

I believe the path should be: imap.gmail.com\[gmail]\sent mail

    + [gmail]
       + Sent Mail

Maybe something else is wrong.

Public WithEvents olkFolder1 As Outlook.Items
Private Sub Application_Quit()
    Set olkFolder1 = Nothing
End Sub
Private Sub Application_Startup()
    'Change the folder path on the following line as needed
    Set olkFolder1 = OpenOutlookFolder("imap.gmail.com\[gmail]\sent mail")
End Sub
Private Sub olkFolder1_ItemAdd(ByVal Item As Object)
    Item.UnRead = False
End Sub
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
    IsNothing = False
  End If
End Function
Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        olkFolder As Outlook.MAPIFolder
    On Error GoTo ehOpenOutlookFolder
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
        If Left(strFolderPath, 1) = "\" Then
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        End If
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkFolder) Then
                Set olkFolder = Session.Folders(varFolder)
                Set olkFolder = olkFolder.Folders(varFolder)
            End If
        Set OpenOutlookFolder = olkFolder
    End If
    On Error GoTo 0
    Exit Function
    Set OpenOutlookFolder = Nothing
    On Error GoTo 0
End Function
 Outlook Group&  Entire Site
 Outlook Group&  Entire Site

Open in new window

David LeeCommented:
I don't use IMAP at all and I'm not certain about how it works in this context.  IMAP is a different beast altogether.  Try this.  Right click on the target folder and select properties.  The path to the folder will be what you see listed next to "Location:" (minus backslashes at the beginning) plus a backslash and the name of the target folder.
r_mayAuthor Commented:
I right clicked on the "Sent Mail" folder.  For location, it just indicated [gmail].  There was not a full path to the folder Sent Mail.  Sounds like it may not work.  Do you have any other suggestions?  

I did notice that when I right click there is a selection for "mark all as read".  If I could somehow create a routine to use this function that maybe another approach.
r_mayAuthor Commented:
Sorry, I was not able to work on this during the week.  Your code for finding the folder path worked fine.  We did have the path correct.  There is something else with the code that is not working.  I am just going to deal with it for now.

Thanks for your help.
David LeeCommented:
You're welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.