Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting specific user created folder

Status
Not open for further replies.

CamaroLT

Programmer
Dec 16, 2002
159
0
0
CA
Based on the top script found at (Code will be pasted below, in case that site goes away) I want to modify it so that the objDestFolder points to a folder based on the received date of all emails, assuming the folder exists. So if I received an email Jan 1, 2017, I want to move that mail to \2017\2017-01. If I receive an email on June 1, 2017 and \2017\2017-06 does not exist, the mail is left untouched in my inbox.

What I'm planning on doing is first moving the Set objDestFolder to within the second "If" block, and set the objDestFolder to point to the folder I want, but not quite sure if this is going to refer to (Logically speaking) \Inbox\YYYY\YYYY-MM or \YYYY\YYYY-MM. If the prior, what do I need to do to get the later? How do I test to see if the folder exists prior to the move?

Would I change that line to:

Code:
Set objDestFolder = objSourceFolder.Folders(Format(objVariant.SentOn,"\yyyy\yyyy-mm"))

??

Here is the code from the site:

Code:
Sub MoveAgedMail()

    Dim objOutlook As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim intDateDiff As Integer
    Dim strDestFolder As String
    
    Set objOutlook = Application
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
    
    ' use a subfolder under Inbox
    Set objDestFolder = objSourceFolder.Folders("Old")

    For intCount = objSourceFolder.Items.Count To 1 Step -1
        Set objVariant = objSourceFolder.Items.Item(intCount)
        DoEvents
        If objVariant.Class = olMail Then
            
             intDateDiff = DateDiff("d", objVariant.SentOn, Now)
             
            ' I'm using 7 days, adjust as needed.
            If intDateDiff > 7 Then

              objVariant.Move objDestFolder
              
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

            End If
        End If
    Next
    
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub



-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
Hi,

You Set the object variable, objDestFolder, and then test to determine if objDestFolder. Exists...
Code:
If Not objDestFolder Is Nothing Then
   'Houston, we have a folder!
End If


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top