cancel
Showing results for 
Search instead for 
Did you mean: 

How to email multiple pdf attachments in outlook email using scripts...?

Former Member
0 Kudos

Hi Stefan & Folks,

I am trying to create a script which can pick up multiple PDF attachments from a path and attach it in Outlook email and send it.

Below is the code i found which is working fine for single attachment...but i just wanna to know how to script for multiple attachments.

I appreciate your help.

***************************************************************

Set objOutlook = CreateObject("Outlook.Application")

Set objMail = objOutlook.CreateItem(0)

objMail.Display   'To display message

objMail.To = "karthikeyan.mohan@emc.com"

objMail.cc = "carthikyen@gmail.com"

objMail.Subject = "Invoice Copy"

objMail.Body = "Please find attached invoice copies"

For Each objFile In objFolder.Files

     strFileExt = LCase(objFSO.GetExtensionName("C:\Users\mohank\Desktop\Inn\*.pdf"))

    If strExt  = strFileExt Then 

           Set objOutlookAttach = objMailItem.Attachments.Add ("C:\Users\mohank\Desktop\Inn\*.pdf"))

    End If 

Next

'("C:\Users\mohank\Desktop\Inn\*.pdf")

objMail.Send   'I intentionally commented this line

Set objMail = Nothing

Set objOutlook = Nothing

***************************************************************

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor
0 Kudos

For WSH you Need to Change vriable declaration

----------------------------

     'Declare the variables

     Dim objFSO

     Dim objFolder

     Dim objFile

     Dim strPath

     Dim strFile

     Dim strFileExt

     Dim NextRow

     Dim objOutlook

     Dim objMail

----------------------------

End Exit script in a different way

----------------------------

     'If the folder does not contain files, exit the sub

     If objFolder.Files.Count = 0 Then

         MsgBox "No files were found...", vbExclamation

         WScript.Quit

     End If

----------------------------

Answers (1)

Answers (1)

holger_khn
Contributor
0 Kudos

Hello.

Have test below code with an Folder on my HDD. That has work for me.

***************************************************************

Sub OL_SendMultiAttach()

     'Set a reference to Microsoft Scripting Runtime by using
     'Tools > References in the Visual Basic Editor (Alt+F11)
    
     'Declare the variables
     Dim objFSO As FileSystemObject
     Dim objFolder As Folder
     Dim objFile As File
     Dim strPath As String
     Dim strFile As String
     Dim strFileExt As String
     Dim NextRow As Long
     Dim objOutlook
     Dim objMail
    
     'Specify the path to the folder
     strPath = "C:\Users\mohank\Desktop\Inn\"
    
     'Create an instance of the FileSystemObject
     Set objFSO = CreateObject("Scripting.FileSystemObject")
    
     'Get the folder
     Set objFolder = objFSO.GetFolder(strPath)
    
     'If the folder does not contain files, exit the sub
     If objFolder.Files.Count = 0 Then
         MsgBox "No files were found...", vbExclamation
         Exit Sub
     End If
      
     Set objOutlook = CreateObject("Outlook.Application")
     Set objMail = objOutlook.CreateItem(0)

     objMail.Display   'To display message
     objMail.To = "karthikeyan.mohan@emc.com"
     objMail.cc = "carthikyen@gmail.com"
     objMail.Subject = "Invoice Copy"
     objMail.Body = "Please find attached invoice copies"
    
     For Each objFile In objFolder.Files
        strFileExt = LCase(objFSO.GetExtensionName(objFile.Path))
        If strFileExt = "pdf" Then objMail.Attachments.Add (objFile.Path)
     Next

     objMail.Send   'I intentionally commented this line
     Set objMail = Nothing
     Set objOutlook = Nothing
     Set objFolder = Nothing
     Set objFSO = Nothing
        
End Sub

***************************************************************

Former Member
0 Kudos

Hi Holger,

Thanks for your time...i used the same code as above to save this as .vbs file and ran it but i am getting error as below.

Please help.

holger_khn
Contributor
0 Kudos

Check line 6 from your script file what is missing