title: Macro for saving emails from outlook - Sender name query
body: <p>So, the organisation I work for requires <em>all</em> project or task related emails to be saved, as part of a formal process, in a specific name format:</p>
  
  <p>YYMMDD-HHmm-SenderName-RecipientName-Subject.msg</p>
  
  <p>We're not allowed to use .pst files for some reason so as you can imagine, doing this individually is tedious in the extreme, and I am lazy, so I decided to try and automate this with a helpful macro. Some intensive googling led me to create the following with help from a website called slipstick:</p>
  
  <pre><code>Option Explicit
  Public Sub SaveMessageAsMsg()
    Dim oMail As Outlook.MailItem
    Dim objItem As Object
    Dim sPath As String
    Dim dtDate As Date
    Dim sName As String
    Dim enviro As String
    Dim strFolderpath As String
  
  
      enviro = CStr(Environ("USERPROFILE"))
  strFolderpath = "DESKTOP LOCATION HERE"
  
     For Each objItem In ActiveExplorer.Selection
     If objItem.MessageClass = "IPM.Note" Then
      Set oMail = objItem
  
    sName = oMail.Subject
    ReplaceCharsForFileName sName, "-"
  
    dtDate = oMail.ReceivedTime
    sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
      vbUseSystem) &amp; Format(dtDate, "-hhnn", _
      vbUseSystemDayOfWeek, vbUseSystem) &amp; "-" &amp; sName &amp; ".msg"
  
    sPath = strFolderpath &amp; "\"
    Debug.Print sPath &amp; sName
    oMail.SaveAs sPath &amp; sName, olMSG
  
    End If
    Next
  
  End Sub
  
  Private Sub ReplaceCharsForFileName(sName As String, _
    sChr As String _
  )
    sName = Replace(sName, "'", sChr)
    sName = Replace(sName, "*", sChr)
    sName = Replace(sName, "/", sChr)
    sName = Replace(sName, "\", sChr)
    sName = Replace(sName, ":", sChr)
    sName = Replace(sName, "?", sChr)
    sName = Replace(sName, Chr(34), sChr)
    sName = Replace(sName, "&lt;", sChr)
    sName = Replace(sName, "&gt;", sChr)
    sName = Replace(sName, "|", sChr)
  End Sub
  </code></pre>
  
  <p>This quite happily saves selected emails in a folder on my desktop (for moving on elsewhere) in the format</p>
  
  <p>YYYYMMDD-HHmm-Subject.msg</p>
  
  <p>Try as I might, however, I can't seem to get the sender's name to go in there as well. I've tried using</p>
  
  <pre><code>Dim sSenderName As String
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Dim sSenderEmailAddress As String
  </code></pre>
  
  <p>With the appropiate additions in the part of the macro which creates the file name:</p>
  
  <pre><code>sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
          vbUseSystem) &amp; Format(dtDate, "-hhnn", _
          vbUseSystemDayOfWeek, vbUseSystem) &amp; "-" *&amp; EXTRA BIT HERE* &amp; "-" &amp; sName &amp; ".msg
  </code></pre>
  
  <p>But all this seems to do is add an extra hyphen into my eventual filename.</p>
  
  <p>Note I'm not bothered about adding recipient into the filename, anything with more than a few recipeints would send it sailing past the character limit.</p>
  
  <p>So, what am I doing wrong? I'm a complete newcomer to VBA and have quite enjoyed this little project, so I'd love to know where I'm fouling up. In my defence I'm a mechanical engineer, and I'm pretty sure that hitting my macro with a hammer won't help.</p>
  
  <p>I'm considering trying to write something which will produce a little popup letting me fill in the pertinent info before saving the email, but that's probably a little way beyond me right now. Gotta walk before I can run.</p>
  
  <p>All tips and comments gratefully received.</p>