Results 1 to 11 of 11
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Send a file to Email

    Hi,

    I have an access file with code to send a file to Outlook which works fine. I copied that code to another access file but it gives me an error. Both on the same computer

    The first part works that creates the excel file. It's the outlook that fails.

    Here is the code

    Private Sub emailTest_Click()


    On Error GoTo emailTest_Click_Err

    Sbj = "Period " & RptDate & " until " & RptDate2
    loc = "c:\temp"
    flnm = loc & "Period" & Month(Now()) & Day(Now()) & ".xlsx"

    DoCmd.OutputTo acOutputQuery, "qryStagedforCRM", acFormatXLSX, flnm, Autostart:=False


    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim MailAttachment As Outlook.Attachments


    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    MailOutLook.Display
    With MailOutLook
    .subject = Sbj
    .To = Me.Email
    .Attachments.Add flnm
    .Display 'This will display the message for you to check and send yourself
    ' .Send ' This will send the message straight away
    End With

    emailTest_Click_Exit:
    Exit Sub


    emailTest_Click_Err:
    MsgBox Error$
    Resume emailTest_Click_Exit
    End Sub

    Here is the error
    Attached Thumbnails Attached Thumbnails Error.png  

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,921
    Please use code tags when posting code.

    Does the other DB have the outlook reference?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    If you are referring to Tools / References in the VBA window then yes I compared that both files have the same lines checked.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,921
    Ok, I am out of ideas if both files know about outlook
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,418
    yes I compared that both files have the same lines checked.
    suggest double check - the error message is saying it cannot find the library. Also looks like you don't have Option Explicit at the top of each module since there are a number of variables you have not declared

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    What happens if you use late binding like this?
    Code:
    Private Sub emailTest_Click()
    On Error GoTo emailTest_Click_Err
    
    
    Sbj = "Period " & RptDate & " until " & RptDate2
    loc = "c:\temp"
    flnm = loc & "Period" & Month(Now()) & Day(Now()) & ".xlsx"
    
    
    DoCmd.OutputTo acOutputQuery, "qryStagedforCRM", acFormatXLSX, flnm, Autostart:=False
    
    
    
    
    Dim appOutLook As Object 'Outlook.Application
    Dim MailOutLook As Object 'Outlook.MailItem
    Dim MailAttachment As Object 'Outlook.Attachments
    
    
    
    
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(0) 'olMailItem
    'MailOutLook.Display '-you doing it later inside the With statement 
    With MailOutLook
    	.subject = Sbj
    	.To = Me.Email
    	.Attachments.Add flnm
    	.Display 'This will display the message for you to check and send yourself
    ' .Send ' This will send the message straight away
    End With
    
    
    emailTest_Click_Exit:
    Exit Sub
    
    
    emailTest_Click_Err:
    MsgBox Error$
    Resume emailTest_Click_Exit
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Click image for larger version. 

Name:	error2.png 
Views:	14 
Size:	72.6 KB 
ID:	50242I get this error

  8. #8
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Do you know what I may be missing.Attachment 50243

  9. #9
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Your attachment doesn't work. What you are probably missing is Option Explicit at the top of all your VBA modules, having that would probably show you that you are missing a control (text box?) named Email on the form where this code is running (Me.Email).

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks. That helped. The issue was the .To=Me.Email so i just removed that line and the email opens with nothing completed in the To: field and we can just enter it in Outlook.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,921
    So why not use the correct control that has the email address ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 10
    Last Post: 03-01-2019, 08:51 AM
  2. Replies: 4
    Last Post: 07-12-2018, 05:38 AM
  3. Replies: 28
    Last Post: 07-05-2017, 01:34 AM
  4. Replies: 2
    Last Post: 03-16-2017, 08:34 PM
  5. Replies: 10
    Last Post: 11-01-2013, 01:20 PM

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