title: VBA using wildcard to attach word docx
body: <p>I'm trying to come up with a code to attach a file based on the email requirements. The file name structure is consistant - Date(YY-MM-DD) followed by a unique number which begins with E and a varying description. Example: "17-08-10 E****** file description.docx". The file being attached will have the unique identifier mentioned in the email body.</p>
  
  <p>I've pieced together codes that i found in my search but i still can't figure out it doesn't attach my document.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Dim OutApp As Object
  Dim OutMail As Object
  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CreateItem(0)
  Dim EmailBody As String
  
  Dim Mail_worksheet As Worksheet
  Dim Mail_worksheet1 As Worksheet
  Set Mail_worksheet = ThisWorkbook.Sheets("Email")
  Set Mail_worksheet1 = ThisWorkbook.Sheets("Send")
  
  Dim strPath As String
  Dim strFilter As String
  Dim strFile As String
  Dim strName As String
  
  Dim c As Integer
  Dim d As String
  
  d = ThisWorkbook.Worksheets("Sheet1").Cells(1, 4)
  
  EmailBody = "&lt;body style=""font-family:Calibri;font-size:16""&gt;Hi," &amp; "&lt;br&gt;&lt;br&gt;" &amp; _
              "Document attached for:" &amp; "&lt;br&gt;&lt;br&gt;"
                  On Error Resume Next
                      For c = 0 To d - 1
                      If Mail_worksheet.Cells(15, 2 + c) = Mail_worksheet1.Cells(7, 6) And Mail_worksheet.Cells(4, 2 + c) Like "E*" Then
                      EmailBody = EmailBody &amp; Mail_worksheet.Cells(4, 2 + c) &amp; "&lt;br&gt;"
  
                          strPath = "D:\My Documents\"      'Edit to your full path
                          strName = Mail_worksheet.Cells(4, 2 + c) 
                          strFilter = "*.docx"
                          strFile = Dir(strPath &amp; Format(Date, "YY-MM-DD") &amp; strName &amp; strFilter)
  
                          While (strFile &lt;&gt; "")
                          If InStr(strFile, "") &gt; 0 Then 'i think my problem is in this line, i'm not sure what to make of it.
                          newItem.Attachments.Add (strPath &amp; strFile)
                          End If
                          strFile = Dir
                          Wend
  
                      End If
                      Next c
  
              EmailBody = EmailBody &amp; "&lt;br&gt;Thank you." &amp; "&lt;br&gt;&lt;br&gt;" &amp; _
                          "Best regards," 
  
  On Error Resume Next
  With OutMail
      .To = ""
      .CC = ""
      .Subject = "Word doc for product - " &amp; Format(Date, "DD MMM YYYY")
      .HTMLBody = EmailBody
  
      .Display
  
  End With
  On Error GoTo 0
  
  Set OutMail = Nothing
  Set OutApp = Nothing
  </code></pre>