Results 1 to 7 of 7
  1. #1
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    84

    Outlook Automation

    Hi all,
    Using Access 365.
    I have a remote server where several users log into and run the Access program. All is running fine, but when the users create documents (Certificates, Test documents, Reports, etc.), the database is saving the documents to the users' folder on the local disk drive. No issue with this.

    The database then uses Outlook to mail these documents to the user's E-Mail as well. I created an E-Mail on the server and the program uses the same E-Mail address to mail the documents for all users.
    I found the below code in one of the forums, and seems to work fine:

    Code:
    Public Function EmailCert(DocName As String)On Error GoTo Errh1:
    
    
        Dim objApp As Object
        Set objApp = CreateObject("Outlook.Application")
        
        Dim objNS As Object
        Set objNS = objApp.GetNamespace("MAPI")
    
    
        Dim olFolder As Object
        Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
            'Open inbox to prevent errors with security prompts
            olFolder.Display
    
    
        Dim objEmail As Outlook.MailItem
        Set objEmail = objApp.CreateItem(olMailItem)
        With objEmail
            .Recipients.Add MailHolder
            .Subject = "Certificate"
            .Body = "See Attached"
            .Attachments.Add DocName
            '.Display
            .Send
        End With
    1 DoEvents
    objApp.Quit
    Exit Function
    BUT:


    1. When "Set olFolder = objNS.GetDefaultFolder(olFolderInbox)" executes, an error is generated saying "Process has failed...", so I had to put this line in a remark state.
    2. When there are more that 1 document mailed directly after another, only the first document is received at the recipient. No error or error messages stating that the mail was not successful, however, if I manually open Outlook on the server, the subsequent documents are sent and received.
    3. I am using the "DoEvents" before closing Outlook. If I omit this line, the E-Mails are not sent.

    Any suggestions to overcome?

    Thanks
    CS

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    I believe if you latebind, which you appear to be doing, you cannot use program enumeration.?
    You have to use the actual values.

    https://learn.microsoft.com/en-us/of...defaultfolders

    If you are adding more than one attachment, you will need to select each in a loop?
    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
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    84
    Hi, Sorry for the delay.
    I entered the actual values (in this case "6") but got the same result. Error message stating "The operation failed."

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Walk through your code, what line does it fail on now?
    Did you change olMailItem for whatever numeric number it needs to be?
    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
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    84
    It fails on the same line (Set olFolder = objNS.GetDefaultFolder(6)"


  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Quote Originally Posted by DC CS View Post
    It fails on the same line (Set olFolder = objNS.GetDefaultFolder(6)
    Don't know what to say TBH, the code works for me except for the fact it closes my existing Outlook instance.
    I would try Early Binding now and see if that works.
    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

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I have been using this function for years and worked for me, could you please give it a try:
    Code:
    Function vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    
    Dim OutApp As Object
    Dim OutMail As Object
     
      
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon 
     
    Set OutMail = OutApp.CreateItem(0)
    
    
    OutMail.To = sTo
    If sCC <> "" Then OutMail.CC = sCC
    If sBcc <> "" Then OutMail.BCC = sBcc
    OutMail.Subject = sSubject
    If sBody <> "" Then OutMail.HTMLBody = sBody 
    If sAttachment <> "" Then OutMail.Attachments.Add (sAttachment)
    
    
    OutMail.Display  'Send | Display
    Set OutMail = Nothing
    Set OutApp = Nothing
    End Function
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 4
    Last Post: 03-14-2022, 12:21 AM
  2. Replies: 3
    Last Post: 05-08-2018, 03:02 PM
  3. Outlook Automation Hanging On Address Book
    By Soule in forum Programming
    Replies: 3
    Last Post: 03-03-2012, 03:04 PM
  4. Replies: 0
    Last Post: 02-23-2011, 10:32 PM
  5. Outlook Object Model question - automation
    By yeah in forum Programming
    Replies: 1
    Last Post: 11-23-2010, 02:05 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