Results 1 to 7 of 7

Thread: Send an Email by clicking a button

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    5

    Send an Email by clicking a button

    Hi, hope you can help me here I'm pulling my hair out with what i thought would be a simple matter.

    I have a small acccess project that i want to automate as much as possible for the end user. So far it's been pretty straight forward. The latest hurdle i've come accross is being able to send an external file as an attatchment to an email recipient. I've had a look at the sendobject cmd but this only appears to work with (1) database objects, ie forms, reports etc and not external files. (2) unless the email to address is in the email programs address book i seem to get an error.

    Someone suggested using mapi, someone else suggested CDO but thats as much information as they gave me. Ive tried with both but keep coming up with errors which i dont entirely understand (mainly because i aint a programmer and i'm trying to pick things up as i go along), the main thrust of the errors was that objects where required - i've added refernces to both mapi and CDO in the references bit and i can see the objects in the browser but i well and truly stuck.

    Some gentle guidance and patience would be greatly appreciated.

    Regards,
    Noel.

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697
    To send an email you can use the below code.

    VB Code:
    1. Option Explicit
    2. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    3. Const SW_SHOWNORMAL = 1
    4.  
    5.  
    6. Private Sub Label2_Click()
    7. ShellExecute Me.hwnd, vbNullString, "mailto:[email protected]", vbNullString, "C:", SW_SHOWNORMAL
    8. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    5
    Thanks for your quick response lintz.

    I've put your code in and sure enough it kicks off and email. Woohoo..
    But, would you mind giving me a little explanation of your code and explaining how i can adapt it for my needs. ie it needs to include a file attactment and it also needs to pick up the "to address" from a table (via a combo box on the active form).

    Regards,
    Noel.

    **Update**
    I've worked out how to put the address in as desired so it's just a little help on the attactment if you would.
    Thanks.
    Last edited by Apollo14; Dec 14th, 2003 at 05:03 PM.

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697
    I think this should work if you're using Outlook. I haven't tested this code as I haven't got Outlook installed on this machine.

    VB Code:
    1. Dim myAttachment As outlook.Attachments
    2. Set myAttachment = olMail.Attachments
    3.  
    4.  
    5. myAttachment.Add "PathToAttachment"

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    5
    Hi Lintz,

    Just got home and tried adding the statement that you gave and i get an error message saying user defined type not defined. I tried type checking it too and after typing outlook. i didnt get the selection that usually appears with recognised object types.

    Regards,
    Noel.

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697
    I've tested this and it works. Make sure you add a reference to Outlook otherwise you'll get errors.

    VB Code:
    1. Dim olMail As Outlook.MailItem
    2. Dim olapp As New Outlook.Application
    3. Dim myAttachment As Outlook.Attachments
    4. Set olMail = olapp.CreateItem(olMailItem)
    5. Set myAttachment = olMail.Attachments
    6.  
    7. olMail.Recipients.Add "[email protected]"
    8. myAttachment.Add "PathToAttachment"
    9. olMail.Send

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    5
    Hi Lintz thanks for your help so far.

    Perhaps i should have mentioned that i dont have outlook installed on my machine - only outlook express. This is because the end project is going out to a user who has only had a basic OS installation done on their PC.

    Is there anyway to use outlook express? I've been doing loads of digging around and have come to the conclusion that it will probly have to be done through CDO.

    I've added refernces to CDO and have tried something like;

    dim Mymsg as cdo.messsage

    mymsg.to = "toaddress"
    mymsg.from = "fromaddress"
    mymsg.send

    When i try to run the routine i get nothing at all. no errors, no email, nothing.

    Any ideas?

    Regards,
    Noel.

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