on 11-25-2015 9:25 AM
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
***************************************************************
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
----------------------------
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
***************************************************************
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.