Results 1 to 7 of 7
  1. #1
    SeniorCitizen is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    32

    Problem with Email from Access

    I am attempting to send an email from an access database via Outlook with a word doc as the body of the email. I have copied the following from another post and changed only the path to my document.\ for a place to start but cant get past the first Dim statement. I get an error 'User defined type undefined'. I also get the same error with other attempts when using the Dim oMail or any other o object.

    I have checked the following libraries:Visual Basic for apps, MS Access 16.0 Obj Lib, MS Word 16.o Obj Lib, MS Office 16.0 Obj Lib and MS Outlook 16.0 Obj Lib.

    I'm obviously missing something. Any thoughts?

    Private Sub BtnSend_Click()
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim itm As Outlook.MailItem
    Dim ID As String
    Dim blnWeOpenedWord As Boolean
    On Error Resume Next

    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    blnWeOpenedWord = True


    End If
    Set doc = wd.Documents.Open _
    (FileName:="E:\Paradise\Paradise Cancellation Policy.doc", ReadOnly:=True)
    Set itm = doc.MailEnvelope.Item
    With itm
    .To = "someone@somewhere.com"
    .Subject = "My Subject"
    .Send
    End With
    doc.Close wdDoNotSaveChanges
    If blnWeOpenedWord Then
    wd.Quit
    End If

    Set doc = Nothing
    Set itm = Nothing
    Set wd = Nothing
    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Can you please try this version that uses late binding:
    Code:
    Dim wd As Object 'Word.Application
    Dim doc As Object 'Word.Document
    Dim itm As Object 'Outlook.MailItem
    Dim ID As String
    Dim blnWeOpenedWord As Boolean
    On Error Resume Next
    
    
    'Set wd = GetObject(, "Word.Application")
    'If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    blnWeOpenedWord = True
    'End If
    Set doc = wd.Documents.Open _
    (FileName:="E:\Paradise\Paradise Cancellation Policy.doc", ReadOnly:=True)
    Set itm = doc.MailEnvelope.Item
    With itm
    .To = "someone@somewhere.com"
    .Subject = "My Subject"
    .Send
    End With
    doc.Close 0 'wdDoNotSaveChanges
    If blnWeOpenedWord Then
    wd.Quit
    End If
    
    
    Set doc = Nothing
    Set itm = Nothing
    Set wd = Nothing
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I tested the original code. Compiles and runs without error and email is sent.

    Please post code between CODE tags to retain indentation and readability.
    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.

  4. #4
    SeniorCitizen is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    32
    Thanks Gicu, I made the suggested changes and it seems to be getting the doc and emailing it. I dont fully understand to early/late binding but I will accept it.

    Great help
    Thanks

  5. #5
    SeniorCitizen is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    32
    Is there any problem with the recipient getting it when the are not using Outlook?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Should not matter what email tool recipient uses. Just as it doesn't when you manually create email directly in Outlook.
    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
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Glad to hear it works for you. Late binding does not need you to set references to other libraries and it is preferred when you have users using a mix of MS Office versions as early binding is not backwards compatible.

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

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

Similar Threads

  1. Replies: 14
    Last Post: 06-19-2020, 03:44 PM
  2. Replies: 2
    Last Post: 05-23-2016, 01:28 PM
  3. Replies: 1
    Last Post: 05-01-2014, 11:37 AM
  4. Replies: 5
    Last Post: 04-25-2013, 10:36 AM
  5. problem with importing email attachments to access
    By funi.t in forum Import/Export Data
    Replies: 1
    Last Post: 12-20-2011, 10:20 AM

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