title: Outlook: Attach a file with a dynamic name
body: <p>Argh, Object doesn't support this property or method error 438!</p>
  
  <p>I haven't tried this but, I think it might work:</p>
  
  <pre><code>objFS.System.IO.Path.GetFileName(fileName)
  if objFS.System.IO.Path.GetFileName(fileName) = "VS12_WID1" Then 
  fileName = AFile.Name
  getFileName = filePath &amp; "/" &amp; fileName
  </code></pre>
  
  <p>I should simplfy it more then try to rename the file.</p>
  
  <pre><code>Sub AddAttachment()
  Dim myAttachments As Outlook.Attachments
  Dim getFile, fileName, filePath As String
  
  
  Set filePath = "F:\"
  Set fileName = "V_W_*_*_.pdf"
  Set getFile = "filePath" &amp; "fileName"
  Set MyApp = CreateObject("Outlook.Application")
  Set myItem = MyApp.CreateItem(0)
  Set myAttachments = myItem.Attachments
  
  With myItem
  .To = "email@mail.com"
  .CC = ""
  .Subject = "test"
  myAttachments.Add getFile
  .ReadReceiptRequested = False
  .HTMLBody = "Report(s) Attached"
  End With
  myItem.Send
  End Sub
  </code></pre>
  
  <p>I'm getting a compile error: Object required highlighting both Sub AddAttachment() and Set filePath.  I feel so close to making this work!</p>
  
  <p>UPDATED CODE:</p>
  
  <pre><code>Sub AddAttachment()
  Dim myAttachments As Outlook.Attachments
  Dim getFileName, fileName, filePath As String
  Dim objFS: Set objFS = CreateObject("Scripting.FileSystemObject")
  
  Set filePath = "F:\"
  Set getFileName = filePath &amp; fileName
  Set MyApp = CreateObject("Outlook.Application")
  Set myItem = MyApp.CreateItem(0)
  Set myAttachments = myItem.Attachments
  
  For Each fileName In filePath
      If fcase(objFS.GetExtensionName(fileName)) = "VS111111_WID111A" Then
          fileName = "VS111111_WID111A.pdf"
          Exit For
      End If
  Next
  
  With myItem
  .To = "email@mail.com"
  .CC = ""
  .Subject = ""
  myAttachments.Add getFileName
  .ReadReceiptRequested = False
  .HTMLBody = "Report(s) Attached"
  End With
  myItem.Send
  End Sub
  </code></pre>
  
  <p>I have enough knowledge to read the script to understand what is going on.  The code I made can only find a fixed file name. How can the file name be made dynamic?</p>
  
  <pre><code>Sub AddAttachment()
  Dim myAttachments As Outlook.Attachments
  Set MyApp = CreateObject("Outlook.Application")
  Set myItem = MyApp.CreateItem(0)
  Set myAttachments = myItem.Attachments
  
  With myItem
  .To = "email@address.com"
  .CC = "email@address.com"
  .Subject = ""
  myAttachments.Add "F:\constantFilenameHas8char_constantFilenameHas7char_variableHas5Int_todaysModifiedDate_variableHas6Int.pdf"
  .ReadReceiptRequested = False
  .HTMLBody = "Report(s) Attached"
  End With
  myItem.Send
  End Sub
  </code></pre>