Results 1 to 7 of 7
  1. #1
    Nuke1096 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2013
    Posts
    53

    Trouble Formatting Outlook Email from Access

    Hello all, I am trying to have my form generate an Outlook email when after I edit some fields on a form and hit the Submit Button, but I keep running into this error....



    "the expression you entered refers to an object that is closed or doesn't exist"

    Let me post my code...


    Sub send_Breakdown()


    Dim appOutLook As Object
    Dim MailOutLook As Object


    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)


    Call OpenOutlook


    mailroster = "MyEmail@whatever.com"

    With MailOutLook
    .To = mailroster
    .Subject = "Breakdown Alert!"
    .body = "A Breakdown Report has been Edited!!! The edited details are listed below..." & Chr(13) & "Vehicle Number: " & Me.VehicleText & Chr(13) & "Date: " & Me.DateText & Chr(13) & "Breakdown Details: " & Me.txtBreakdownDetails & Chr(13) & "Breakdown Location: " & Me.TextAddress & Chr(13) & "City/Town: " & Me.TextCity & Chr(13) & "Additional Location Notes: " & Me.Detail_TextBox
    .send
    End With

    Set appOutLook = Nothing
    Set MailOutLook = Nothing


    End Sub


    I'm calling this sub from the actual button command sub. This is what I have in my command button sub.

    MsgBox "Breakdown Successfully Edited!"
    Call send_Breakdown
    DoCmd.Close acForm, "Form_Breakdown_Edit"



    The error is definitely coming from the .body line, since I commented that line out and it works fine then. Even if I trim the code down to something like this, I still get the same error.

    .body = "Random Text" & Chr(13) & "Additional Location Notes: " & Me.Detail_TextBox

    I made sure that the name of the Text Box is correct of course, but I cant seem to figure out why I get that error. Any help is greatly appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You might get rid of the form reference to isolate whether it's the .Body or the form reference causing the problem. Make sure the underscore isn't actually a space. Try Me.VehicleText instead of Me.Detail_TextBox to determine if it's that particular control. You're sure it gets past the other mail related lines? With late binding, I wouldn't expect olMailItem to work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Nuke1096 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2013
    Posts
    53
    I tried VehicleText and a few other controls, so it's definitely not that. I also commented out the entire .body line and it works then, so it has to be coming from there. I don't get it.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Instead of Chr(13), try: vbCrLf

    Or

    Chr(13) & Chr(10)

    Did you try constructing the .Body string without any variables, just concatenate literal text. If that works, then one of the variables is issue. Try including them one at a time.
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'll leave you in June7's capable hands.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Nuke1096 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2013
    Posts
    53
    I did just a simple .body command like

    .body = "test message.."

    and that works fine, so its the rest of the body line.

    I have also tried just a simple cmd like...

    .body = Me.TextBoxName

    That still gave me the error, and I did try multiple fields/text boxes to make sure it wasnt just that one. I also didnt include Chr(13) when I tested it that way, and still got the error, so I dont think its that.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Coding for text to wrap to a new line requires both codes: Chr(13) & Chr(10) (in that order) or use the vbCrLf constant.

    I tested constructing a .Body string with vbCrLf and literal text and it works.
    Code:
    Sub send_Breakdown()
    Dim appOutLook As Object
    Dim MailOutLook As Object
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
    .To = "MyEmail@whatever.com"
    .Subject = "Breakdown Alert!"
    .Body = "A Breakdown Report has been Edited!!! The edited details are listed below..." & vbCrLf & "Vehicle Number: " & vbCrLf & "Date: " & vbCrLf & "Breakdown Details: " & vbCrLf & "Breakdown Location: " & vbCrLf & "City/Town: " & vbCrLf & "Additional Location Notes: "
    '.Send
    .Display
    End With
    Set appOutLook = Nothing
    Set MailOutLook = Nothing
    End Sub
    Back to the issue of references to controls. Access isn't finding them. If you want to provide db for analysis, follow instructions at bottom of my post.


    Why the Call OpenOutlook line?
    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. Replies: 5
    Last Post: 04-25-2013, 10:36 AM
  2. Replies: 7
    Last Post: 10-11-2012, 02:13 PM
  3. FORMATTING Outlook Email in BODY of Access code
    By taimysho0 in forum Programming
    Replies: 7
    Last Post: 11-28-2011, 11:04 AM
  4. Send email from Access thru Outlook
    By ZMAN in forum Forms
    Replies: 2
    Last Post: 11-27-2010, 06:10 PM
  5. Replies: 3
    Last Post: 09-01-2010, 08:43 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