Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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.

http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_23227680.html

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?

Thanks,
Roger
0
r_may
Asked:
r_may
  • 4
  • 3
1 Solution
 
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
0
 
edster9999Commented:
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
    Next
  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

0
 
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

imap.gmail.com
    + [gmail]
       + Sent Mail

Maybe something else is wrong.

Thanks.
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
    Item.Save
End Sub
 
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    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
    Else
        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)
            Else
                Set olkFolder = olkFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkFolder
    End If
    On Error GoTo 0
    Exit Function
ehOpenOutlookFolder:
    Set OpenOutlookFolder = Nothing
    On Error GoTo 0
End Function
 
 
 
    
 
 
 
 Outlook Group&  Entire Site
 
 
 Outlook Group&  Entire Site
 
 

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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.
0
 
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.
0
 
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

0
 
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.
0
 
David LeeCommented:
You're welcome.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now