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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.
David LeeCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.