以下代码搜索上述文件夹中的文件,并通过outlook发送搜索到的文件。但我需要为它添加更多条件。
Sub CheckandSend()
Dim obMail As Outlook.MailItem
Dim irow As Integer
Dim dpath As String
Dim pfile As String
`dpath = "xxxx"
`'' loop through all files and send mail
irow = 1
Do While Cells(irow, 1) <> Empty
'' get file name in column A
pfile = Dir(dpath & "\*" & Cells(irow, 1) & "*")
'' check file exist and pdf file
If pfile <> "" And Right(pfile, 3) = "pdf"
Then
Set obMail = Outlook.CreateItem(olMailItem)
With obMail
.To = "xxx@domain.com"
.Subject = "123"
.BodyFormat = olFormatPlain
.Body = "123"
.Attachments.Add (dpath & "\" & pfile)
.Send
End With
End If
irow = irow + 1
Loop
End sub
答案 0 :(得分:0)
您可以简化代码,使用Windows脚本搜索所有PDF文件的目录(和子目录),然后发送找到的每个文件。
这会处理您的第二个问题,但我不明白您的第一个问题:
a)如何在同一文件夹中使用同名文件?
b)到目前为止,你还没有展示过自己尝试过的东西。
Sub SO()
Const masterFolder As String = "C:\Users\Macro Man\Files"
Dim files, file
files = Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & masterFolder & "\*.pdf"" /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
For Each file In files
With Outlook.CreateItem(0)
.To = "xxx@domain.com"
.Subject = "123"
.BodyFormat = olFormatPlain
.Body = "123"
.Attachments.Add CStr(file)
.Send
End With
Next
End Sub