Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jan 2005
    Location
    Wichita, Kansas, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    email function (2007)

    I am using the function email as below. My questions are:
    1 With Office 2007, is Outlook called by a different term? I get an error if Outlook is not already open, but works fine if it is open.
    2.) Preceding the upgrade to 2007, if I don't have an attachment, I get an "Invalid use of null" error

    Function SendMessages(QueryName As String, _
    FieldName As String, _
    ReplyTo As String, _
    Optional AttachmentPath As String)
    Dim MyDB As DAO.Database
    Dim MyRS As DAO.Recordset
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim TheAddress As String
    'this code supplied by John Hutchinson to allow the replies to go
    'to someone other than me, like John Doe
    Dim strReplyaddress As String
    'from the next line on, this is the previous code
    Set MyDB = CurrentDb
    Set MyRS = MyDB.OpenRecordset(QueryName)
    MyRS.MoveFirst

    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")

    Do Until MyRS.EOF
    ' Create the e-mail message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    TheAddress = MyRS.Fields(FieldName)
    With objOutlookMsg
    ' Add the To recipients to the e-mail message.
    Set objOutlookRecip = .Recipients.Add(TheAddress)
    objOutlookRecip.Type = olTo

    ' Add the Cc recipients to the e-mail message.
    If (IsNull(Forms!frmMail!CCAddress)) Then
    Else
    Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
    objOutlookRecip.Type = olCC
    End If

    ' Set the Subject, the Body, and the Importance of the e-mail message.
    .Subject = Forms!frmMail!Subject
    .Body = Forms!frmMail!MainText
    .Importance = olImportanceHigh 'High importance

    If Not ReplyTo = "" Then
    .ReplyRecipients.Add ReplyTo
    End If
    'Add the attachment to the e-mail message.
    If Not IsNull(AttachmentPath) Then
    Set objOutlookAttach = .Attachments.Add(AttachmentPath)
    End If

    ' Resolve the name of each Recipient.
    'For Each objOutlookRecip In .Recipients
    ' objOutlookRecip.Resolve
    ' If Not objOutlookRecip.Resolve Then
    ' objOutlookMsg.Display
    ' End If
    'Next
    .Send
    End With
    MyRS.MoveNext
    Loop
    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing
    End Function

    Thanks in advance for the help!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: email function (2007)

    1) In the future, please mention what error you get (error number, error message) - just stating "I get an error" doesn't give us much to help you.
    Does it work if you use the following?
    <code>
    Set objOutlook = CreateObject("Outlook.Application.12")
    </code>
    2) The line
    <code>
    If Not IsNull(AttachmentPath) Then
    </code>
    should be
    <code>
    If Not AttachmentPath = "" Then
    </code>
    The argument AttachmentPath is defined as a String; a String can never be Null.

  3. #3
    2 Star Lounger
    Join Date
    Jan 2005
    Location
    Wichita, Kansas, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: email function (2007)

    Sorry, Hans, for the lack of clarity. You are always so perceptive that I forget you do need SOME information to work with.
    Yes, if Outlook is not already open, I get "Application-defined or object-defined error"

    I substituted your suggested code in mine, such that it now says:
    'Set objOutlook = CreateObject("Outlook.Application")
    Set objOutlook = CreateObject("Outlook.Application.12")

    But I still get the "Application-defined or object-defined error"

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: email function (2007)

    Does the error occur at that line, or at another line - if so, which one?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •