Results 1 to 2 of 2

Thread: send email body

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    7

    Question send email body

    I would like to send an attachment to a user and include some (body) text to it.

    I'm putting it into the subject, but its getting too long.

    Code im using is...

    application.dialogues(xldialogsendmail).show user, subject

    Can anyone help?

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: send email body

    For this I would try and use Microsoft Outlook directly in the code rather than the sendmail..

    Irrespective of the Application you are using this can be accomplished by adding the Microsoft Outlook reference into the Tools, References in the Visual Basic Editor (ALT+F11) just scroll down until you find it.

    VB Code:
    1. Function WriteEmail(pTo As String, pSubject As String, pBody As String, pIsAttach As Boolean, _
    2.   Optional pCC As String = "", Optional pBCC As String = "", Optional pAttachLoc As String = "") As Boolean
    3.   Dim obOT As Outlook.Application
    4.   Dim obMI As Outlook.MailItem
    5.   Dim obAT As Outlook.Attachment
    6.   WriteEmail = False 'set the function to fail initially
    7.   'check for attachment first
    8.   If pIsAttach And pAttachLoc = "" Then
    9.     MsgBox "You have requested an attachment but have not supplied a filename", vbCritical, "No Attachement"
    10.     GoTo ExitProc
    11.   End If
    12.   'check the remaining required fields, in not populated then error out
    13.   If pTo = "" Then
    14.     MsgBox "Please supply at least one address to send to.", vbCritical, "No To Address"
    15.     GoTo ExitProc
    16.   End If
    17.   If pSubject = "" Then
    18.     MsgBox "Please supply a subject for this email.", vbCritical, "No Subject"
    19.     GoTo ExitProc
    20.   End If
    21.   If pBody = "" Then
    22.     MsgBox "Please supply a body of this email.", vbCritical, "No Body"
    23.     GoTo ExitProc
    24.   End If
    25.  
    26.   'Get Outlook if it exists, if not attempt to create an instance
    27.   On Error GoTo CreateInst
    28.   Set obOT = GetObject(, "Outlook.Application")
    29.   GoTo WriteMail
    30.  
    31. CreateInst:
    32.   On Error GoTo NoOutlook
    33.   'Create the instance of outlook, if this fails at this point either there is not outlook installed
    34.   'or there is an application error.
    35.   Set obOT = CreateObject("Outlook.Application")
    36.   GoTo WriteMail
    37.  
    38. WriteMail:
    39.   On Error GoTo NoOutlook
    40.   Set obMI = obOT.CreateItem(olMailItem)
    41.   obMI.To = pTo
    42.   If pCC <> "" Then obMI.CC = pCC
    43.   If pBCC <> "" Then obMI.BCC = pBCC
    44.   If pIsAttach Then
    45.     Set obAT = obMI.Attachments.Add(pAttachLoc)
    46.   End If
    47.   obMI.Subject = pSubject
    48.   obMI.Body = pBody
    49.   obMI.Send
    50.   WriteEmail = True
    51.  
    52. ExitProc:
    53.   'close down the outlook object
    54.   On Error Resume Next
    55.   Set obAT = Nothing
    56.   Set obMI = Nothing
    57.   Set obOT = Nothing
    58.   On Error GoTo 0
    59.   Exit Function
    60.  
    61. NoOutlook:
    62.   MsgBox Err.Description, vbCritical, Err.Number
    63.   GoTo ExitProc
    64. End Function

    I use this myself in an access database..

    You will however get a lot of confirmation notices due to outlook security, but it is pretty robust as it stands..

    to use it do something like

    VB Code:
    1. If Not WriteEmail("PersonTo","subject","Body",True, , , "Filename") Then
    2.   Msgbox "Email Failed"
    3. End If
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

Posting Permissions

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



Click Here to Expand Forum to Full Width