Email Attachments VBA CDOUpload ImagesLink Harvey Abernathy 6 months ago
I have an access routine that sends emails just fine and as long as I use the full path to the file in quotes, will send attachments. However, if I try to use a variable for the file name it will not work, Example: .AddAttachment "F:\actualfilename" - works .AddAttachment (VariableName) - will not work Any ideas why a variable will not work. The variable = the full filename.
Kevin Robertson 6 months ago
Remove the parentheses.
.AddAttachment VariableName
And this (Kevin's fast response) is why I sometimes wait 24 hours before answering any posts. LOL
Harvey Abernathy 6 months ago
Kevin - Removing parentheses does not fix the problem.
Richard - I have tried to make the variable contain the full filename with and without quotes. I have tried the variable inside parentheses and without parentheses. When using a variable for the filename the routine does not give an error, it just does not send the email. replace the variable with "F:\filename" it works just fine.
That's really weird. It shouldn't matter. Did you DIM your variable as a String? Can I see the whole block of code?
Harvey Abernathy 6 months ago
Here is the Access Subroutine:
Public Sub SendMail(pLine, smptServer, userName, userPassword, userEmailTo, userEmailFrom, attachmentTotal, subjectLine, attachmentArray())
Dim mail As Object ' CDO.MESSAGE
Dim config As Object ' CDO.Configuration
Dim x As Integer
Set mail = CreateObject("CDO.Message")
Set config = CreateObject("CDO.Configuration")
With config.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value = 1 'SMTP Auth (For Windows Auth set this to 2)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusestartls").Value = True 'Enable STARTLS Authentication
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl").Value = False 'Disable send as SSL
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smptServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value = 25
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername").Value = userName
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value = userPassword
.Item(CdoMailHeader.cdoDispositionNotificationTo).Value = userEmailFrom
.Item(CdoMailHeader.cdoReturnReceiptTo).Value = userEmailFrom
.Update
End With
Set mail.Configuration = config
With mail
.From = userEmailFrom
.Subject = subjectLine
.To = userEmailTo
.Textbody = pLine
If attachmentTotal > 0 Then
For x = 0 To attachmentTotal
MsgBox ".AddAttachment " & attachmentArray(x) 'For testing only to make sure the variable = the full filename
.AddAttachment attachmentArray(x)
Next
End If
.Send
End With
Set config = Nothing
Set mail = Nothing
End Sub
For x = 0 To attachmentTotal
S = attachmentArray(x)
.AddAttachment S
Next
Harvey Abernathy 6 months ago
I also tried to use a variable instead of the array. I used your idea but still does not work: s is dim as string
If attachmentTotal > 0 Then
For x = 0 To attachmentTotal
s = attachmentArray(x)
MsgBox ".AddAttachment " & s 'For testing only to make sure the variable = the full filename
.AddAttachment s
Next
End If
.Send
Harvey Abernathy 6 months ago
Here is the calling routine:
Private Sub SendEmail_Click()
Dim pLine As String
Dim userName As String
Dim smptServer As String
Dim userPassword As String
Dim userEmailTo As String
Dim userEmailFrom As String
Dim attachmentTotal As Integer
Dim subjectLine As String
Dim attachmentArray(2) ' I tried this as string also
attachmentArray(0) = "F:\UCMN6300_usermanual.pdf"
attachmentArray(1) = "F:\SessionTalkIPs.txt"
'MsgBox "Send Email" 'Testing
pLine = "Send Email from Stellarc on " & Date & " at " & Time
smptServer = "192.168.2.25"
userName = "xxxx" 'Removed
userPassword = "xxxxxxx" 'Removed
userEmailTo = "[email protected]"
userEmailFrom = "[email protected]"
attachmentTotal = 1
subjectLine = "Email Succeeded"
SendMail pLine, smptServer, userName, userPassword, userEmailTo, userEmailFrom, attachmentTotal, subjectLine, attachmentArray()
End Sub
Harvey Abernathy 6 months ago
This is the silliest thing. I put an on error Goto so I could see the error. The error was could not find the file. Looking at the calling routine, I mistakenly got a 'N' in the filename. Once I fixed that everything worked just fine. I am really sorry for wasting your time for such a stupid mistake on my part.
Haha. It's something simple and/or stupid 99% of the time. Glad you fixed it. Be careful with "on error" handlers. Sometimes you WANT to see that error message! I've got a whole video on this topic on my list.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.