0

I need to attach variable PDFs from a transport folder.

Sub send_attachent()

Dim OutApp As Object
Dim OutMAil As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMAil = OutApp.CreateItem(0)

strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
"Regards,<br>"

On Error Resume Next
With OutMAil
    .TO = "julia.naydenova@hitachirail.com"
    .CC = "jean.ash@hitachirail.com"
    .BCC = ""
    .Subject = "AT300-HRD-00"
    .Display
    .HTMLBody = strbody & .HTMLBody
    .Attachments.Add "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER\AT300-HRD-00031 Test.pdf"
    'You can add other files also like this
    '.Attachments.Add ("C:\test.txt")
End With
    
MsgBox "Email Sent"
On Error GoTo 0
    
Set OutMAil = Nothing
    
End Sub

I need to send variable files, so whatever I put in the folder to be attached on the email. With the file name in the macro I can only send one file.

Community
  • 1
  • 1
Emily
  • 11
  • 2

2 Answers2

0

The Outlook object model doesn't provide anything for monitoring files in a folder on the disk. You need to create a file-watcher which can monitor files in a folder and create a new mail item when a file is added to the folder. See VBA monitor folder for new files for more information on that.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Loop through files in a folder with Dir.

Option Explicit

Sub send_all_PDF_folder()

Dim outApp As Object
Dim outMail As Object

Dim strbody As String

Dim filePath As String
Dim fileName As String

Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)

strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
  "Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
  "Regards,<br>"

filePath = "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER"

With outMail

    .To = "someone@somewhere.com"
    .CC = "someoneCC@somewhere.com"
    
    .Subject = "AT300-HRD-00"
    .Display
    .HtmlBody = strbody & .HtmlBody
    
    fileName = dir(filePath & "\*.pdf")
    
    Do While fileName <> ""
        .Attachments.Add filePath & fileName
        fileName = dir
    Loop
    
End With

Set outMail = Nothing
Set outApp = Nothing

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52