从列出的文件夹和子文件夹中搜索文档的VBA代码?

时间:2018-02-20 02:05:16

标签: excel vba excel-vba

我列出了搜索和附加文档到电子邮件的代码。名称值列在A24列中,一切正常,但下面的文件夹路径不查看子文件夹;如果文件列在主文件夹中,它只会拾取并附加文件。

我试过把'& " \"'在路径的公式中,但它仍然没有工作。任何人都可以建议任何替代方法脚本到文件夹和子代码?

我当前的VBA代码如下:

Sub AttachandSendEmail()
Dim obMail As Outlook.MailItem
Dim irow As Integer
Dim dpath As String
Dim pfile As String

'file path
**dpath = "C:\Users\document_location_folder"**

'looping through all the files and sending an mail

irow = 24

Do While Cells(irow, 1) <> Empty


'pikcing up file name from column C
 pfile = Dir(dpath & "\*" & Cells(irow, 1) & "*")


'checking for file exist in a folder and if its a pdf file

If pfile <> "" And Right(pfile, 1) = "pdf" Then

Set obMail = Outlook.CreateItem(olMailItem)
With obMail
.To = "email@comapny.com"
.Subject = "O/S Blanace"
.BodyFormat = olFormatPlain
.Body = "Please see attached files"

Do While Cells(irow, 1) <> Empty
    'pikcing up file name from column C
    pfile = Dir(dpath & "\*" & Cells(irow, 1) & "*")
    'checking for file exist in a folder and if its a pdf file

    If pfile <> "" And Right(pfile, 1) = "pdf" Then
        .Attachments.Add (dpath & "\" & pfile)
    End If

    'go to next file listed on the C column
    irow = irow + 1
Loop

.Send
End With

End Sub

0 个答案:

没有答案