VBA使用通配符附加单词docx

时间:2017-08-10 12:31:45

标签: wildcard outlook-vba email-attachments

我试图根据电子邮件要求提出附加文件的代码。文件名结构是一致的 - 日期(YY-MM-DD)后跟一个以E开头的独特数字和不同的描述。示例:" 17-08-10 E ******文件description.docx"。附加的文件将具有电子邮件正文中提到的唯一标识符。

我将搜索中找到的代码拼凑在一起,但我仍然无法确定它是否附加了我的文档。

这是我的代码:

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 = "<body style=""font-family:Calibri;font-size:16"">Hi," & "<br><br>" & _
            "Document attached for:" & "<br><br>"
                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 & Mail_worksheet.Cells(4, 2 + c) & "<br>"

                        strPath = "D:\My Documents\"      'Edit to your full path
                        strName = Mail_worksheet.Cells(4, 2 + c) 
                        strFilter = "*.docx"
                        strFile = Dir(strPath & Format(Date, "YY-MM-DD") & strName & strFilter)

                        While (strFile <> "")
                        If InStr(strFile, "") > 0 Then 'i think my problem is in this line, i'm not sure what to make of it.
                        newItem.Attachments.Add (strPath & strFile)
                        End If
                        strFile = Dir
                        Wend

                    End If
                    Next c

            EmailBody = EmailBody & "<br>Thank you." & "<br><br>" & _
                        "Best regards," 

On Error Resume Next
With OutMail
    .To = ""
    .CC = ""
    .Subject = "Word doc for product - " & Format(Date, "DD MMM YYYY")
    .HTMLBody = EmailBody

    .Display

End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

1 个答案:

答案 0 :(得分:0)

无论如何,设法搞清楚了。不得不为这部分创建新的整数。

 $.ajax({
  type: "POST",
  url: 'data.php',
  data: {lastSerial: true},
  dataType: 'json',
  success: function(data){
      var objDiv = document.getElementById("result");

      $.each(data, function(key, value) {
          objDiv.innerHTML += "New transaction: " + value.SerialNo + ' ' + value.TimeStamp + "<br>";
      });
      objDiv.scrollTop = objDiv.scrollHeight;
     } // end success
 });

感谢niton的指导!