• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

Sending automatic emails with Visual Basic (in Excel)

I have a Microsoft Excel spreadsheet with a whole lot of Visual Basic doing some stuff with forms etc.  I would like to be able to send an automatic email (using Netscape Messenger) to a specified user, containing the value of a couple of variables in my code.  Is there anyway I can do this?

1 Solution
Richie_SimonettiIT OperationsCommented:
If netscape messenger is your default mail client:

See what I got from VBPJ 101 techtips 10th edition:

Fill In the E-Mail Fields
ShellExecute is one of the most flexible Win32 APIs. Using ShellExecute, you can pass any filename,
and if the file?s extension is associated to a registered program on the user?s machine, the correct
application opens and the file is played or displayed.
    In the February 1998 101 Tech Tips supplement, Jose Rodriguez Alvira showed ShellExecute?s Internet
power (<Create Internet-Style Hyperlinks>). If you pass an HTTP URL, the user?s default 32-bit Web browser
opens and connects to the site. If you pass an e-mail address that has been prefaced with <mailto:>,
the user?s default 32-bit e-mail client opens a new e-mail note with the address filled in.
    Here?s how to automatically get a lot more than just the e-mail addresses filled in. If you want
to include a list of CC recipients, BCC recipients, or your own subject text or body text, you can create
a string variable, add the list of primary addresses (separated by semicolons), then a question mark
character and element strings prefaced like this:

For CCs (carbon copies): &CC= (followed by list)
For blind CCs: &BCC= (followed by list)
For subject text: &Subject= (followed by text)
For body text: &Body= (followed by text)
To add an attachment: &Attach= (followed by a valid file path within chr(34)?s)

To use this trick, create a new VB project, add a form, and add six textboxes and a button (cmdSendIt).
Paste this into the form?s Declarations section:

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
Private Const SW_SHOWNORMAL = 1

Paste this code into the button?s Click event:

Private Sub cmdSendIt_Click()
    Dim sText As String
    Dim sAddedText As String
    If Len(txtMainAddresses) Then
         sText = txtMainAddresses
    End If
    If Len(txtCC) Then
         sAddedText = sAddedText & "&CC=" & txtCC
    End If
    If Len(txtBCC) Then
         sAddedText = sAddedText & "&BCC=" & txtBCC
    End If
    If Len(txtSubject) Then
         sAddedText = sAddedText & "&Subject=" & txtSubject
    End If
    If Len(txtBody) Then
         sAddedText = sAddedText & "&Body=" & txtBody
    End If
    If Len(txtAttachmentFileLocation) Then
         sAddedText = sAddedText & "&Attach=" & _
              Chr(34) & txtAttachmentFileLocation & Chr(34)
    End If
    sText = "mailto:" & sText
    ' clean the added elements
    If Len(sAddedText) <> 0 Then
         ' there are added elements, replace the first
         ' ampersand with the question character
         Mid$(sAddedText, 1, 1) = "?"
    End If
    sText = sText & sAddedText
    If Len(sText) Then
         Call ShellExecute(Me.hWnd, "open", sText, _
              vbNullString, vbNullString, SW_SHOWNORMAL)
    End If
End Sub

You can?t have spaces between the ampersands and tags, or between the tags and the equal signs. You
don?t have formatting options, so body text will be one paragraph. However, when you use this technique,
program errors are e-mailed to you with full details, and you can create real e-mail applets in a just
a few seconds. It beats automating a full e-mail program.
    In addition, almost all this functionality is possible in HTML MailTo tags. Here is a sample:

Richie_SimonettiIT OperationsCommented:
Optionally, you could use this excellent/free tool:
CaraFrowAuthor Commented:
I have added this to the declarations:

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
Private Const SW_SHOWNORMAL = 1

and created this Function which I call when someone clicks on a form button to submit a request.  I am getting an error that says method or data member not found for the Me.hWnd bit.  What should this be?:

''''Sends email to nominated email addresses from Congiguration tab''''
Function send_Email(ByVal sUser As String, ByVal sTeam As String, ByVal sTeamLeader As String, ByVal dtStartDate As Date, ByVal dtEndDate As Date, ByVal sDescription As String, ByVal sEnvironment As String, ByVal sBuild As String, ByVal sDatabase As String)
    'declare variables and constants
    Dim sEmailAddress As String
    Dim sText As String
    Dim sSubject As String
    Dim sBody As String
    'assign helpful values to constants
    Const counter4 As Integer = 4
    Const counter7 As Integer = 7

    sEmailAddress = "mailto:" & Configuration.Cells(counter4, counter7).Value
    sSubject = "Environment/Database booking request"
    sBody = "Booking requested by " & sUser & Chr(10) & "Team: " & sTeam & Chr(10) & "Team Leader: " & sTeamLeader & Chr(10) & "Environment: " & sEnvironment & Chr(10) & "Build #: " & sBuild & Chr(10) & "Database: " & sDatabase & Chr(10) & "Reason for booking request: " & sDescription

    'Concatenate email address and Subject and Body
    sText = sEmailAddress & "?Subject=" & sSubject & "&Body=" & sBody
    'If there are some details in the variable, open the email
    If Len(sText) Then
       Call ShellExecute(Me.hWnd, "open", sText, _
              vbNullString, vbNullString, SW_SHOWNORMAL)
    End If
End Function
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CaraFrowAuthor Commented:
Also, what is the story with the shell32.dll file?
Valliappan ANSenior Tech ConsultantCommented:
Richie_SimonettiIT OperationsCommented:
Don't use me.hwnd if this fucntion is in a module. Only Form/class knows Me keyword.
Would you try with:

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

Private Sub Command1_Click()
ShellExecute 0, vbNullString, _
                 "mailto:" & Text1(0).Text & _
                 "?subject=" & Text1(3).Text & _
                 "&body=" & Text1(4).Text & _
                 "&CC=" & Text1(1).Text & _
                 "&BCC=" & Text1(2).Text, _
                 0&, 0&, 1
End Sub
Hi CaraFrow,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Richie_Simonetti's comment(s) as an answer.

CaraFrow, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as answer

E-E Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now