I've listed code where it searches and attaches documents to email. The name value is listed in column A24, Everything works just fine except the below folder path does not look into subfolders; it only picks up and attaches the files if it's listed in the main folder.
I tried putting '& "\"' in the formula for path, but still it didn't work. Can anyone suggest any alternate way to script into folders and subfoders?
My current VBA code is below:
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