Search code examples
vbaoutlook

Format the SentOn to display only date


I am trying to construct a filename from an Outlook item.

I have code to save the file with the date (from mySelectedItem.SentOn) in the filename .

I don't need hours, minutes and seconds, like "19062019 105228-from-to-subject".

Dim msgFileName As String
msgFileName = mySelectedItem.SentOn & "-" & _
              mySelectedItem.SentOnBehalfOfName & "-" & _
              mySelectedItem.To & "-" & _
              mySelectedItem.Subject

I only need "19062019-from-to-subject".


Solution

  • This will Work

    All you have to use Format function on the Date. Like Format(date, "ddmmyyy")

    Dim msgFileName As String
    msgFileName = Format(mySelectedItem.SentOn,"ddmmyyyy") & "-" & _
                  mySelectedItem.SentOnBehalfOfName & "-" & _
                  mySelectedItem.To & "-" & _
                  mySelectedItem.Subject