Sending automatic emails with Visual Basic (in Excel)

Posted on 2002-05-17
Last Modified: 2013-11-25
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?

Question by:CaraFrow
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 7017913
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:

LVL 16

Expert Comment

ID: 7017916
Optionally, you could use this excellent/free tool:

Author Comment

ID: 7019432
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 7019436
Also, what is the story with the shell32.dll file?

Expert Comment

by:Valliappan AN
ID: 7021055
LVL 16

Accepted Solution

Richie_Simonetti earned 200 total points
ID: 7022970
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
LVL 49

Expert Comment

ID: 7817834
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

Expert Comment

ID: 7967278
Comment from expert accepted as answer

E-E Admin

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

717 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