Results 1 to 10 of 10
  1. #1
    dcdimon's Avatar
    dcdimon is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Location
    Bradenton, FL
    Posts
    71

    Question Need clarification on VBA script used to send email via outlook


    I have two questions regarding a script I made for sending out email in Access.

    (1) I have a working VBA script that opens the Outlook application, if it's not already open, builds the email and then sends it, sort of. When Outlook isn't open the email gets stuck in the outbox until the application is actually running. Then when a send occurs the email is sent. If Outlook is open, then the email gets created and sent all at once.

    I've done a search on the web and can't find an answer to whether Outlook needs to be open for the send to occur. Is the script only opening outlook to create the email and set it up to send the next time Outlook opens? Or is there a problem in my script that's causing this to happen.

    (2) In my script I have both .Display and .Send. If I comment out, or remove the .Display, my script breaks and fails with an 'Application-defined or object-defined error" message and the .Send is highlighted. Am I missing something in the script that's causing me to need both of these items?

    My code is below. Any insight would be appreciated.

    Code:
    Option Compare Database
    Option Explicit
    Sub CreateEmail()
    
    
    'Generate email
    
    
        Dim OutApp As Object
        Dim OutMail As Object
    
    
    ' Opens outlook app and then closes it, if not open
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(olMailItem)
    
    
            With OutMail
            
                .To = "someemail@email.com"
                .CC = "moreemail@email.com"
                .BCC = ""
                
                'create subject line with file name, user and time submitted.
                .Subject = "Feedback on " & Left(CurrentProject.Name, Len(CurrentProject.Name) - 6) & _
                " from " & UserFullName & " - " & Date
                
                '.attachments.Add attmt
                .Body = "New feedback: " & vbCrLf & Forms!FeedbackF!Commenttxt & vbCrLf & "Left by " & UserFullName & vbCrLf _
                & vbCrLf & "This is an automated message, please don't reply directly to the user." & _
                vbCrLf & vbCrLf & "Palletizing QSB DBS - " & Now()
                
                
                'Need both .display and .send to autosend.
                
                .Display
                .Send
    
    
            
            End With
    
    
    
    
    End Sub
    DD

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    This code
    Code:
     Set OutApp = CreateObject("Outlook.Application")
    opens a copy of outlook. So, no, it doesn't have to be running already.

    The problem may be that you are hitting the end of the procedure and Outlook is closing before it gets a chance to send the message.

    1) You can try executing DoEvents() after the "End With" to allow the operating system a chance to process the send command.
    2) You can also put a little "For" loop at that spot to pause a second or two.
    3) You can open a hidden form and use a form-boundl variable and a form timer for the outlook application and leave it running for some length of time before closing it.

  3. #3
    dcdimon's Avatar
    dcdimon is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Location
    Bradenton, FL
    Posts
    71
    DAL: I've tried putting in some sort of 'pause' function but the email still gets stuck in the outbox until Outlook is opened by the user. I think I'm just going to give up on having this be a truly automated email and request users start outlook before or after they use the database. It's the only way I can get this to work.

    I still am looking for an answer to my second question. I can't get the script to work using just the .Send function. I have to include the .Display and .Send or I get an error message.

    Can anyone shed light on why I have to do this? I've seen scripts out there that just use the .Send and the response that it worked. I cannot understand why I can't do that in my scripts.

    Thanks for the assist.

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Oh, what's the specific error message if you just .send? In the scripts I've seen, .Display and .Send are mutually exclusive. I notice that one of the MS-posted routines for this had a .Save before .Send, so try adding that, just in case, and commenting out the .Display for the test.

    It's also possible, if you are in a decent-sized organization, you might be running into something that is an organizational security rule or something. I'd expect certain other symptoms, though, since it would be pretty stupid security that let the email get created and only prevented it from sending until the person next started Outlook.

  5. #5
    dcdimon's Avatar
    dcdimon is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Location
    Bradenton, FL
    Posts
    71

    Thumbs up Missing .Bodyformat caused email sending issues.

    Here's what I discovered, quite by accident.

    I found a sample database that would send out email. I loaded it and it worked as expected even without outlook open. I copied the script over to my email module and commented out what didn't apply and changed the email options (To, Subject..etc) to work with my email setup. Then I started commenting out some of the script. When I commented out the line that read .BodyFormat = olFormatRichText I got the 'object-defined' error. When I put it back into the script my email was created and sent out without any problems.

    My guess is that without this, Outlook would open, but since it didn't know how to handle the formatting the email got stuck. Once Outlook opened, it applied the default format then off the email went. Is this possible?

    My script is the same except for the addition of: .BodyFormat = olFormatRichText


    Code:
    Option Compare Database
    Option Explicit
    Public Sub CreateEmail()
    
    
    'Generate email
        'Dim appOutLook As Outlook.Application
        'Dim MailOutLook As Outlook.MailItem
        
        Dim OutApp As Object
        Dim OutMail As Object
    
    
    ' Opens outlook app and then closes it, if not open
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(olMailItem)
    
    
           With OutMail
                .BodyFormat = olFormatRichText <==============new to script.
                '.To = "someemail@email.com"
                .CC = "someemail@email.com"
                .BCC = ""
                
                'create subject line with file name, user and time submitted.
                .Subject = "Feedback on " & Left(CurrentProject.Name, Len(CurrentProject.Name) - 6) & _
                " from " & UserFullName & " - " & Date
                
                '.attachments.Add attmt
                
                .HTMLBody = "New feedback: " & vbCrLf & Forms!FeedbackF!Commenttxt & vbCrLf & "Left by " & UserFullName & vbCrLf _
                & vbCrLf & "This is an automated message, please don't reply directly to the user." & _
                vbCrLf & vbCrLf & "Palletizing QSB DBS - " & Now()
                
                
                'Need both .display and .send to autosend.
                .Send
                '.Display
            End With
    
    
    End Sub
    DD

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,604
    Sounds like a reasonable conclusion. http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    I have always included that line in code to send email. Now I know why it's needed.
    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.

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Cool find!

    Is there HTML in your data that becomes the body of the email? Or is that required even when the body is plain text?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,604
    Use .Body or .HTMLBody.

    I have .HTMLBody with .BodyFormat set to olFormatRichText and it still works. But the text string has neither RTF nor HTML code tags.

    Click on the triangle next to OlBodyFormat in that article to see 4 constants.
    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.

  9. #9
    dcdimon's Avatar
    dcdimon is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Location
    Bradenton, FL
    Posts
    71
    Quote Originally Posted by Dal Jeanis View Post
    Cool find!

    Is there HTML in your data that becomes the body of the email? Or is that required even when the body is plain text?

    From what I found, the .Bodyformat needs to be included regardless of how you're getting the body text. I experimented with just script entered text (no html coding) and with pulling body text from a form control and without the .Bodyformat the email was created, but wasn't sent until Outlook opened.

    I haven't found anything that says you have to use the .Bodyformat function when sending email, but that's how it seems to be functioning. Of course, when Outlook is open that's not an issue; the email goes right out then.

    DD

  10. #10
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Okay, that's weird. In theory, the minute you create an Outlook application, outlook is "open".

    Hmmm. I wonder if the .Bodyformat is somehow setting immediate focus to the Outlook app, whereas the other methods somehow aren't. Did you try using the .Save?

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

Similar Threads

  1. Replies: 7
    Last Post: 11-19-2013, 01:58 PM
  2. Replies: 5
    Last Post: 04-25-2013, 10:36 AM
  3. Send email in Outlook with attachment
    By kelkan in forum Programming
    Replies: 1
    Last Post: 02-01-2013, 10:31 PM
  4. Send email from Access thru Outlook
    By ZMAN in forum Forms
    Replies: 2
    Last Post: 11-27-2010, 06:10 PM
  5. Replies: 1
    Last Post: 03-02-2010, 10:51 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