Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Help > DevNet >
Back to Access Developer Network    Comments List
Email Attachments VBA CDO Upload Images   Link   Email  
Harvey Abernathy 
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 
Remove the parentheses.
.AddAttachment VariableName
Richard Rost 
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 
And this (Kevin's fast response) is why I sometimes wait 24 hours before answering any posts. LOL
Harvey Abernathy 
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 
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 
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 
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 
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 
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 
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 
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 in Access Developer Network.
 

 
 
 

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
PCResale.NET
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
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 3/28/2024 4:42:21 AM. PLT: 0s