title: Send all files in folder as separate attachments
body: <p>I am trying to attach all files in a folder in separate emails using a modified version of this code from <a href="https://www.slipstick.com/developer/macro-send-files-email/" rel="nofollow noreferrer">https://www.slipstick.com/developer/macro-send-files-email/</a>.</p>
  
  <pre><code>Dim fldName As String
  
  Sub SendFilesbyEmail()
  ' From http://slipstick.me/njpnx
  Dim sFName As String
  
  i = 0
  fldName = "C:\Users\Test"
  sFName = Dir(fldName)
  Do While Len(sFName) &gt; 0
  'filter for only *.txt
    If Right(sFName, 4) = ".txt" Then
        Call SendasAttachment(sFName)
        i = i + 1
    End If
    sFName = Dir
  Loop
  MsgBox i &amp; " files were sent"
  End Sub
  
  Function SendasAttachment(fName As String)
  
  Dim olApp As Outlook.Application
  Dim olMsg As Outlook.MailItem
  Dim olAtt As Outlook.Attachments
  
  Dim localfName As String
  Dim localfldName As String
  
  Set olApp = Outlook.Application
  Set olMsg = olApp.CreateItem(0) ' email
  Set olAtt = olMsg.Attachments
  
  ' attach file
  olAtt.Add (fldName &amp; fName)
  localfName = fName
  
     ' send message
  With olMsg
    .Subject = "PDF Import: " &amp; Left(localfName, Len(localfName) - 4)
    .To = "test@test.com"
    .HTMLBody = "Test"
    .Send
  End With
  End Function
  </code></pre>
  
  <p>The issue comes with trying to put the file name into the email subject.</p>
  
  <pre><code>.Subject = "PDF Import: " &amp; Left(localfName, Len(localfName) - 4)
  </code></pre>
  
  <p>If I remove localfName from the subject, to send a generic subject for all emails, the code works fine.</p>
  
  <p>When I put either fName or localfName (my attempt to debug the issue), the first email sends, but on the second iteration, the DIR function returns a file name from a different folder, and the code breaks because the file it's trying to attach can't be found.</p>