Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
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 
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
Richard Rost
6 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
6 months ago
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.
Richard Rost
6 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
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
Richard Rost
6 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
         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 = ""
  userEmailFrom = ""
  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.
Richard Rost
6 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
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.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard

Blog RSS Feed    Twitter

YouTube Channel    LinkedIn
Copyright 2023 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/10/2023 4:58:52 AM.