Results 1 to 9 of 9
  1. #1
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20

    Attachments to Emails

    Hi there.



    I am attempting to use the following code to attach a .pdf file to an email:-
    #######################################
    Dim strTo As String
    Dim strSubject As String
    Dim strBody As String
    Dim strPathAttach As String
    Dim objOutlook As Object
    Dim objMailItem As Object
    Const olMailItem As Integer = 0
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMailItem = objOutlook.CreateItem(olMailItem)
    strTo = Forms!f2_Advised!Text43
    objMailItem.To = strTo
    strSubject = Forms!f2_members_details!Full_name & " - Charitable status statement"
    objMailItem.subject = strSubject
    strBody = Forms!f2_email_text!Email_Body_Text
    objMailItem.body = strBody

    strPathAttach = Forms!f2_Advised!Text29 '= "R:\charity\Statements\Parker David Fi-31Aug12_email.pdf" '
    With objMailItem.Attachments
    .Add strPathAttach
    End With

    objMailItem.Send
    ##############################################
    I am able to send out the email ... but .... the attachment will not attach
    I get the error
    "Cannot find the file"
    "Verify the path and filename are correct"
    This happens on the .Add strPathAttach line of code.

    If I cut and paste the file name (as shown in the comments) into my Windows File Explorer, it loads Adobe and displays the file perfectly.

    In desperation I have tried all sorts of permutations of enclosing qoutes , putting in equals signs ... adding commas ... pure desperation! ... all to no avail.

    Can anyone point me to the cause of this aberration please?

  2. #2
    GeekInOhio's Avatar
    GeekInOhio is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2012
    Posts
    25
    Try changing:

    Code:
    strPathAttach = Forms!f2_Advised!Text29
    to

    Code:
    strPathAttach = Forms!f2_Advised.Text29

  3. #3
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    Code:
    strPathAttach = Forms!f2_Advised.Text29
    [/QUOTE]


    I was very excited when I saw this suggestion, it being about the only permutation that I hadn't thought of trying.

    Regret, it didn't work. ... but many thanks for spending the time looking at it for me.

    I think I might have to resort to 'snailmail'

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I tested code to send email with PDF attachment and it worked perfectly.

    Try a test of code with literal string instead of the textbox variable.

    This is my code:
    Code:
    Sub emailTest()
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
        .BodyFormat = olFormatRichText
        .To = "email address here"
        ''.cc = ""
        ''.bcc = ""
        .Subject = "test"
        .HTMLBody = "PDF attached"
        .Attachments.add ("C:\test.pdf")
        .DeleteAfterSubmit = True 'to not save in sent bin
        ''.Display
        .Send
    End With
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    On thing you can do is put a msgbox Forms!f2_Advised!Text29 line right before the strpathattach = line, to verify that Forms!f2_Advised!Text29 really does contain the value it should.

    John

  6. #6
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    Quote Originally Posted by John_G View Post
    On thing you can do is put a msgbox Forms!f2_Advised!Text29 line right before the strpathattach = line, to verify that Forms!f2_Advised!Text29 really does contain the value it should.

    John
    John, A debug.print confirms that the file name is thre and correct.
    ... also, if I cut and paste that into the Windows7 File explorer, it brings up the file in Adboe.
    So I am pretty confident that that is OK.

  7. #7
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    June7.

    I like your clean code and will, if I ever get this thing going, implement it .
    It is so much easier to read.

    However, it still will not work for me.

    I am beginning to wonder if there is some problem in my Outlook2010 environment that is refusing to accept the attachment

    Any Outlook experts out there?

    Many thanks for the help already given ... it is really appreciated.

  8. #8
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    June7

    Thank you for your input.
    I liked the clean appearance and elegance of your code, and rewrote mine accordingly.
    (True plagiarism) ... and I love it --- Ta.

    However, I still had exactly the same problem , and couldn't shake it.

    Bizarre as it is, I eventually got it going by adding this

    Dim FileName as String
    FileName = Forms!f2_Advised.Text29
    and
    .Attachments.Add FileName

    .... and now it works

    For some reason it wanted go go via a simple variable!

    Many thanks for your code ... and all of your time and effort... all of you

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    My code is essentially the same code you already had but looks cleaner only because I am not using a lot of variables. I routinely don't use variables if they will be referenced only once. Reference to the form control is usually adequate. However, you encountered situation where that was not true. Still odd but as long as it works...

    If Outlook had been the issue, error message probably would have been different. For a while I couldn't get this code to work on my home pc. Kept telling me Outlook needed to be configured. Just tried again today and it worked so something in my pc environment changed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Emails with a Macro
    By srcacuser in forum Access
    Replies: 5
    Last Post: 05-02-2012, 11:49 AM
  2. Automatic Emails
    By smit2215 in forum Queries
    Replies: 1
    Last Post: 03-10-2011, 01:23 PM
  3. Access emails
    By Gargen in forum Access
    Replies: 5
    Last Post: 08-04-2010, 01:10 PM
  4. Automatic emails.
    By motherboard in forum Queries
    Replies: 3
    Last Post: 05-04-2010, 11:03 AM
  5. Emails from Access
    By dbn00bz in forum Access
    Replies: 0
    Last Post: 12-07-2009, 07:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums