Solved

Automated e-mail from VB application

Posted on 2002-06-18
7
128 Views
Last Modified: 2010-05-02
I'm using the following code line to open Outlook with a new message containing a To: address and subject line.

ShellExecute Me.hwnd, "Open", "MailTo:myname@mailserver.com?Subject=A certain subject line", "", "", 0&


It works fine, but I'd like to be able to specify an attachment also, so the user has as little to do as possible. Is there a way to do this?

Thanks.
0
Comment
Question by:rdolivaw
7 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7090013
Here is some code that will open Outlook and mail with attachments

Sub Email()
   
    Dim objOL As Object
    Dim objEMail As Object
   
    Set objOL = CreateObject("Outlook.Application") 'opens Outlook
   
    Set objEMail = objOL.CreateItem(olMailItem) ' opens new email
    With objEMail
        .Recipients.Add "me@me.com" 'recipient's email address
        .Subject = "Your details " & x 'subject of the email
        .Body = "Details"
        'To send an attachment
        .Attachments.Add ("C:\my documents\file.txt")
        .Send
    End With
   
    Set objEMail = Nothing
       
End Sub

This should do it if I did not forget anything.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7090210
have a look at this free an easy mail object: http://www.freevbcode.com/ShowCode.Asp?ID=109
0
 
LVL 10

Accepted Solution

by:
arana earned 50 total points
ID: 7090213
outlook supports file attachments via shellexecute, but other mailers probably dont support it:

pCh = "mailto:myname@mailserver.com?subject=your_subject&body=your_body&file=" & chr(34) & "c:\autoexec.bat" & chr(34)


ShellExecute Me.hwnd, "Open", pCh, "", "",
0&
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7090378
Listening...
0
 

Author Comment

by:rdolivaw
ID: 7090650
arana:

Thanks for the help. The other two were extremely helpful, too, but since all my users have Outlook as their default e-mail program, your solution was easier than those.

Thanks to you other responders, too. Most helpful and interesting information.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7090714
Arana, i did try your code but i didn't get any attachment.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7090720
Sorry, i did try with Outlook Express ;)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question