-1

The following is a VBScript (.vbs) that processes all MS Outlook .msg files dropped on it, opening each .msg in MS Outlook to retrieve the required information, then renaming the file to ReceivedTime + Subject + SenderName. For example, 27102017 123241 AM - Meeting agenda - George.msg

I am having difficulty changing the date format from DDMMYYYY to YYYYMMDD. I have replaced varNewFileName = ReplaceIllegalCharacters(olkMessage.ReceivedTime... with varNewFileName = ReplaceIllegalCharacters(Format(olkMessage.ReceivedTime, "yyyymmdd-hhnnss")... but when I run the script the new file name is returned without a date and time. For example, - Meeting agenda - George.msg

If anyone could assist with the date formatting I would be very grateful.

George

On Error Resume Next

Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName
Set olkApp = GetObject(,"Outlook.Application")
If TypeName(olkApp) = "Nothing" Then
    Set olkApp = CreateObject("Outlook.Application")
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each varFile In WScript.Arguments
    Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
    varNewFileName = ReplaceIllegalCharacters(olkMessage.ReceivedTime & " " & olkMessage.Subject & " From " & olkMessage.SenderName & " To " & olkMessage.To) & ".msg"
    Set objFile = objFSO.GetFile(varFile)
    objFile.Name = varNewFileName
Next
Set objFile = Nothing
Set objFSO = Nothing
Set olkMessage = Nothing
Set olkApp = Nothing
WScript.Quit

Function ReplaceIllegalCharacters(strSubject)
    Dim strBuffer
    strBuffer = Replace(strSubject, ":", "")
    strBuffer = Replace(strBuffer, "\", "")
    strBuffer = Replace(strBuffer, "/", "")
    strBuffer = Replace(strBuffer, "?", "")
    strBuffer = Replace(strBuffer, Chr(34), "'")
    strBuffer = Replace(strBuffer, "|", "")
    ReplaceIllegalCharacters = strBuffer
End Function
georgemackenzie
  • 171
  • 2
  • 4
  • 19

2 Answers2

0

Try this:

Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
tempArray = split(olkMessage.ReceivedTime), " ") 
tempArray(0) = Year(tempArray(0)) & Right("0" & Month(tempArray(0)),2) & Right("0" & Day(tempArray(0)),2)
myDateTime = join(tempArray, " ") 
varNewFileName = ReplaceIllegalCharacters(myDateTime & " " & olkMessage.Subject & " From " & olkMessage.SenderName & " To " & olkMessage.To) & ".msg" Set objFile = objFSO.GetFile(varFile)

So what I did here was take the value for the received date/time of the message and split it into an array on the space in the string, which gives a date element and a time element in the resulting array.

Rearrange the values in the date portion then join the array back together and pass this value into the function instead of the object's date time attribute.

Should do what you need. Apologies if there are any typos as I'm using my mobile...

Let me know if you need anything clarified.

Dave
  • 4,328
  • 2
  • 24
  • 33
0

Thanks a million Dave your response pointed me in the right direction! After doing some reading I realised that VBScript uses a different FormatDateTime(date,format) sytax which is why I couldn't get the date to format to yyyymmdd. My updated code below:

For Each varFile In WScript.Arguments   
    Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
    strReceived = year(olkMessage.ReceivedTime) & _
            right("0" & month(olkMessage.ReceivedTime), 2) & _
            right("0" & day(olkMessage.ReceivedTime), 2) & " " & _
            right("0" & hour(olkMessage.ReceivedTime), 2) & _
            right("0" & minute(olkMessage.ReceivedTime), 2) & _
            right("0" & second(olkMessage.ReceivedTime), 2)
    varNewFileName = ReplaceIllegalCharacters(strReceived & " " & olkMessage.Subject & " From " & olkMessage.SenderName & " To " & olkMessage.To) & ".msg"
    Set objFile = objFSO.GetFile(varFile)
    objFile.Name = varNewFileName   
Next
georgemackenzie
  • 171
  • 2
  • 4
  • 19