Reorder myItem.CreationTime in Outlook VBA script

We currently have an Outlook macro (attached )which appends the email's author and date/time created to the subject line for use in easier retrieval in archival storage.  The problem is the standard myItem.CreationTime value is displayed mm/dd/yyyy.  We would like it to display yyyy/mm/dd.  Any help would be appreciated.

Sub Date_Stamp_Subject()
Dim Messages As Selection
Dim myItem As MailItem
Dim strSubject As String
Set Messages = ActiveExplorer.Selection
'Set myItem = GetCurrentItem()
    If Messages.Count = 0 Then
        Exit Sub
    End If
    For Each myItem In Messages
        myItem.Subject = myItem.CreationTime & " ---" & myItem.Sender & "--- " & myItem.Subject
        myItem.Save
    Next
End Sub

Open in new window

M_PattonAsked:
Who is Participating?
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.

omgangIT ManagerCommented:
Here's the output from the Immediate window

7/30/2015 8:03:25 AM ---Experts Exchange--- Group Discussion Digest
2015/07/30 ---Experts Exchange--- Group Discussion Digest

OM Gang


Sub Date_Stamp_Subject()
On Error GoTo Err_Date_Stamp_Subject

    Dim Messages As Selection
    Dim myItem As MailItem
    Dim strSubject As String
    Dim dteCreateTime As Date
    Set Messages = ActiveExplorer.Selection
    'Set myItem = GetCurrentItem()
        If Messages.Count = 0 Then
            Exit Sub
        End If
        For Each myItem In Messages
            'myItem.Subject = myItem.CreationTime & " ---" & myItem.Sender & "--- " & myItem.Subject
            'myItem.Save
            strSubject = myItem.CreationTime & " ---" & myItem.Sender & "--- " & myItem.Subject
            Debug.Print strSubject
            
            strSubject = Format(myItem.CreationTime, "yyyy/mm/dd") & " ---" & myItem.Sender & "--- " & myItem.Subject
            Debug.Print strSubject
        Next
        
Exit_Date_Stamp_Subject:
    Set myItem = Nothing
    Set Messages = Nothing
    Exit Sub
    
Err_Date_Stamp_Subject:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_Date_Stamp_Subject
    
End Sub

Open in new window

0
M_PattonAuthor Commented:
When I first ran the tweaked updated code (added h:mm:ss tt to the date format), nothing happened.  I restarted Outlook and got the following error when I selected one or more messages:

91, Object variable or With block variable not set

I used what was written without the tweak, and I still got the error.

So, I tweaked what I originally had with ideas from the updated code and it works perfectly.  (Working code below.)

Sub Date_Stamp_Subject()
Dim Messages As Selection
Dim myItem As MailItem
Dim strSubject As String
Set Messages = ActiveExplorer.Selection
'Set myItem = GetCurrentItem()
    If Messages.Count = 0 Then
        Exit Sub
    End If
    For Each myItem In Messages
        myItem.Subject = Format(myItem.CreationTime, "yyyy/mm/dd h:mm:ss AMPM") & " ---" & myItem.Sender & "--- " & myItem.Subject
        myItem.Save
    Next
End Sub

Open in new window

0

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
M_PattonAuthor Commented:
When I first ran the tweaked updated code (added h:mm:ss tt to the date format), nothing happened.  I restarted Outlook and got the following error when I selected one or more messages:

91, Object variable or With block variable not set

I used what was written without the tweak, and I still got the error.

So, I tweaked what I originally had with ideas from the updated code and it works perfectly
0
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
Outlook

From novice to tech pro — start learning today.

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.