Free Lessons
Fast Tips
Topic Index
Home   Courses   Seminars   Templates   TechHelp   Forums   Help   Contact   Join   Order   Logon  
Home > Help > DevNet >
Access Developer Network

Back to Access Developer Network

Email Attachments VBA CDO Upload Images   Link  
Harvey Abernathy 
9 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
9 months ago
Remove the parentheses.
.AddAttachment VariableName
Richard Rost
9 months ago
Are you putting the variable name inside of paratheses like that? try removing them. It's a method, so I don't believe you need them.
Richard Rost
9 months ago
And this (Kevin's fast response) is why I sometimes wait 24 hours before answering any posts. LOL
Harvey Abernathy
9 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.
Richard Rost
9 months ago
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
9 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("").Value = 1         'SMTP Auth (For Windows Auth set this to 2)
     .Item("").Value = True        'Enable STARTLS Authentication
     .Item("").Value = False           'Disable send as SSL
     .Item("").Value = 2
     .Item("").Value = smptServer
     .Item("").Value = 25
     .Item("").Value = userName
     .Item("").Value = userPassword
     .Item(CdoMailHeader.cdoDispositionNotificationTo).Value = userEmailFrom
     .Item(CdoMailHeader.cdoReturnReceiptTo).Value = userEmailFrom
  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)
       End If
  End With
  Set config = Nothing
  Set mail = Nothing
End Sub
Richard Rost
9 months ago
Problem could be sending the array element. Try:

Dim S as String

And then in your loop:

         For x = 0 To attachmentTotal
           S = attachmentArray(x)
          .AddAttachment S
Harvey Abernathy
9 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
       End If
Harvey Abernathy
9 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 = ""
  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
9 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.
Richard Rost
9 months ago
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.

This thread is now closed. If you wish to comment, start a NEW discussion, below.

Back to Access Developer Network Comments

Start a NEW Conversation
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
Subscribe to Access Developer Network
Get notifications when this page is updated

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.

Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
My Account
My Courses
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

Copyright 2023 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 9/23/2023 12:44:53 AM.