Skip to Content
avatar image
Former Member

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

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Nov 25, 2015 at 01:55 PM

    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

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

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 25, 2015 at 10:22 AM

    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

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

    Add comment
    10|10000 characters needed characters exceeded