'File exists through Dir() validation, yet it doesn't get attached on mailing macro
I have a macro that can send multiple mails with attachments to an specific user, this had no problem when I sent 1 file to 1 user, but I modified it so it loops to send one mail with many attachments (so it doesn't fills the recipients inbox). The problem now is that it's not attaching any file at all.
First I ask the user to specify the folder where the files are stored so I get the cadenaDirectorio
(path) then it iterates through the users emails to get the address, then I search for each file hasn't been sent yet and finally validate with Dir()
function if the file exists or not, which correctly returns true or false but it doesn't attach anything never.
For Each llave In dictMov.Keys
Set objMail = outlookApp.CreateItem(olMailItem) 'MAIL OBJECT
'GET EMAIL
For filaIndiceFuenteM = 2 To filaFuenteUltimaM
If llave = planillaFuenteM.Cells(filaIndiceFuenteM, "K") Then
mailJefe = planillaFuenteM.Cells(filaIndiceFuenteM, "M")
End If
Next filaIndiceFuenteM
For filaIndiceFuente = 437 To filaFuenteUltima
Set objMail = outlookApp.CreateItem(olMailItem) 'MAIL CREATION
'GET MAIL
For filaIndiceFuenteM = 2 To filaFuenteUltimaM
If llave = planillaFuenteM.Cells(filaIndiceFuenteM, "K") Then
mailJefe = planillaFuenteM.Cells(filaIndiceFuenteM, "M")
End If
Next filaIndiceFuenteM
'GET DOCS TO SEND
If llave = planillaFuente.Cells(filaIndiceFuente, "L") And planillaFuente.Cells(filaIndiceFuente, "AA") = "SIN DESP" Then
'LOOK FOR THE FILES
archivoFuente = Dir
cadenaFuente = ""
archivoFuente = Dir(cadenaDirectorio & planillaFuente.Cells(filaIndiceFuente, "C") & "*.pdf")
cadenaFuente = cadenaDirectorio & archivoFuente
If Dir(archivoFuente) = "" Then
noExistentes = noExistentes & vbNewLine & planillaFuente.Cells(filaIndiceFuente, "D")
Else
objMail.Attachments.Add archivoFuente
End If
End If
Next filaIndiceFuente
If noExistentes <> "" Then MsgBox noExistentes
objMail.To = mailJefe 'TO
objMail.Subject = "Contrato de Trabajo" 'SUBJECT
objMail.Body = "THIS IS A TEST MAIL" 'MAIL BODY
objMail.Display
mailJefe = ""
pendientesDoc = ""
noExistentes = ""
Next llave
Solution 1:[1]
Please give this a try :
For Each llave In dictMov.Keys
Set objMail = outlookApp.CreateItem(olMailItem) 'MAIL OBJECT
'GET EMAIL
For filaIndiceFuenteM = 2 To filaFuenteUltimaM
If llave = planillaFuenteM.Cells(filaIndiceFuenteM, "K") Then
mailJefe = planillaFuenteM.Cells(filaIndiceFuenteM, "M")
End If
Next filaIndiceFuenteM
For filaIndiceFuente = 437 To filaFuenteUltima
Set objMail = outlookApp.CreateItem(olMailItem) 'MAIL CREATION
'GET MAIL
For filaIndiceFuenteM = 2 To filaFuenteUltimaM
If llave = planillaFuenteM.Cells(filaIndiceFuenteM, "K") Then
mailJefe = planillaFuenteM.Cells(filaIndiceFuenteM, "M")
End If
Next filaIndiceFuenteM
'GET DOCS TO SEND
If llave = planillaFuente.Cells(filaIndiceFuente, "L") And planillaFuente.Cells(filaIndiceFuente, "AA") = "SIN DESP" Then
'LOOK FOR THE FILES
archivoFuente = Dir
cadenaFuente = ""
archivoFuente = Dir(cadenaDirectorio & planillaFuente.Cells(filaIndiceFuente, "C") & "*.pdf")
cadenaFuente = cadenaDirectorio & archivoFuente
If Dir(archivoFuente) <> "" Then
'IF TRUE (EXISTS) THEN ATTACH
objMail.Attachments.Add cadenaFuente
Else
'IF FILE NEEDS TO BE SENT BUT DOESN'T EXISTS SHOW VARIABLE AS MSGBOX LATER
noExistentes = noExistentes & vbNewLine & planillaFuente.Cells(filaIndiceFuente, "D")
End If
End If
Next filaIndiceFuente
If noExistentes <> "" Then MsgBox noExistentes
objMail.To = mailJefe 'TO
objMail.Subject = "Contrato de Trabajo" 'SUBJECT
objMail.Body = "THIS IS A TEST MAIL" 'MAIL BODY
objMail.Display
mailJefe = ""
pendientesDoc = ""
noExistentes = ""
Next llave
You had to specify the path of your file and not only the name and your if statment was also wrong, I think you did 2 typo =)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |