I want to create excel vba with the below criteria:
Search for PDF files with names mentioned in column A1, A2... Multiple... in a directory and if found send that file via outlook with the searched file attachment. If file not found ignore that file and move to the next file.
The below vba I got is showing error "Type Mismatch" at pfile = pfile + 1 and loop is not executing. Let me know where i am going wrong
Sub CheckandSend()
Dim obMail As Outlook.MailItem
Dim irow As Integer
Dim dpath As String
Dim pfile As String
`'' directory that contains files
`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
pfile = pfile + 1
Loop
End Sub
答案 0 :(得分:0)
pfile
是一个字符串。所以,如果你需要连接字符串,你需要使用&amp;而不是符号。
很可能你需要增加细胞数量,如下所示:
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