Results 1 to 5 of 5
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating an Outlook Mail Item (2003)

    How do you open a new Outlook mail message from within Access?
    I've tried using

    <pre>Public Sub CreateOutlookMail()
    Dim appOutlook As Outlook.Application
    Dim con As Outlook.ContactItem
    On Error GoTo ErrorHandler
    Set appOutlook = GetObject(, "Outlook.Application")
    Set con = appOutlook.CreateItem(olMailItem)
    con.Display
    ErrorHandlerExit:
    Set appOutlook = Nothing
    Exit Sub
    ErrorHandler:
    'Outlook is not running; open Outlook with CreateObject
    If Err.Number = 429 Then
    Set appOutlook = CreateObject("Outlook.Application")
    Resume Next
    Else
    MsgBox "Error No: " & Err.Number & "; Description: "
    Resume ErrorHandlerExit
    End If
    End Sub</pre>


    but I get the Error No: 13 with no description

  2. #2
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Creating an Outlook Mail Item (2003)

    I have the following code- I do not remember from where I got it:


    Function SendMail( _
    Recipient As String, _
    Subject As String, _
    Message As String, _
    Attachment As String) As Boolean
    '
    'You need to add a reference to the Outlook nn type library (where nn is the version you use). You can do this in any module by selecting Tools/References...

    'The function takes 4 string arguments:
    'Recipient is a list of e-mail adresses separated by semicolons ;
    'Subject will become the subject of the e-mail
    'Message will become the body of the e-mail
    'Attachment is the full path and file name of the file you want to attach.

    'The function returns True if it succeeds, False if it fails.

    Dim objOL As Outlook.Application
    Dim objMI As Outlook.MailItem
    Dim blnNotActive As Boolean
    Dim intPos1 As Integer, intPos2 As Integer

    SysCmd acSysCmdSetStatus, "A moment please. Your e-mail message is being sent."
    DoCmd.Hourglass True

    ' Check whether Outlook is active
    On Error Resume Next
    Set objOL = GetObject(, "Outlook.Application")
    blnNotActive = (Err <> 0)

    If blnNotActive Then
    ' If not, we start Outlook
    Err.Clear
    Set objOL = CreateObject("Outlook.Application")
    End If

    On Error GoTo Err_Mail

    ' Create e-mail message
    Set objMI = objOL.CreateItem(olMailItem)
    With objMI
    intPos2 = 1
    intPos1 = InStr(intPos2, Recipient, ";")
    Do While intPos1 > 0
    .Recipients.Add Mid$(Recipient, intPos2, intPos1 - intPos2)
    intPos2 = intPos1 + 1
    intPos1 = InStr(intPos2, Recipient, ";")
    Loop
    .Recipients.Add Mid$(Recipient, intPos2)
    .Subject = Subject
    .Body = Message
    .Attachments.Add Attachment, olByValue
    .Send
    End With
    SendMail = True

    Exit_Mail:
    ' Release object memory
    On Error Resume Next
    If Not (objMI Is Nothing) Then objMI.Close olDiscard
    Set objMI = Nothing
    If blnNotActive And Not (objOL Is Nothing) Then objOL.Quit
    Set objOL = Nothing
    SysCmd acSysCmdClearStatus
    DoCmd.Hourglass False
    Exit Function

    Err_Mail:
    SendMail = False
    Resume Exit_Mail
    End Function

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

    Re: Creating an Outlook Mail Item (2003)

    If you want to create a mail item, you shouldn't declare the variable as Outlook.ContactItem but as Outlook.MailItem. But if you just want to display a new mail item, you only need

    Public Sub CreateOutlookMail()
    On Error GoTo ErrHandler
    DoCmd.SendObject EditMessage:=True
    Exit Sub

    ErrHandler:
    If Not Err = 2501 Then
    MsgBox Err.Description, vbExclamation
    End If
    End Sub

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating an Outlook Mail Item (2003)

    Thank you Zave,
    Your post and Hans' made me realise that, in my subroutine, I should have declared the con variable as MailItem rather than ContactItem. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating an Outlook Mail Item (2003)

    Thank you Hans, yours is the shortest/easiest route <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

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