我是vba初学者..
我想用以下标准创建excel vba:
在目录中搜索列A1,A2 ...多个...中提到的名称的PDF文件,如果找到,则通过outlook使用搜索到的文件附件发送该文件。如果找不到文件,则忽略该文件并移至下一个文件。
它应该一次只通过outlook发送一个文件,然后是第二个,依此类推。
答案 0 :(得分:0)
试试这个
Sub CheckandSend()
Dim obMail As Outlook.MailItem
Dim irow As Integer
Dim dpath As String
Dim pfile As String
'' directory that contains files
dpath = "xxx"
'' 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@xxx.com"
.Subject = "xxxx"
.BodyFormat = olFormatPlain
.Body = "xxxx"
.Attachments.Add (dpath & "\" & pfile)
.Send
End With
End If
'' go to next file
pfile = pfile + 1
Loop
End Sub
请记住在excel中添加引用Microsoft Outlook库以生成邮件项目。