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
Solved

Sending automatic emails with Visual Basic (in Excel)

Posted on 2002-05-17
8
489 Views
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?

Thanks.
0
Comment
Question by:CaraFrow
8 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
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:

feedback@smithvoice
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7017916
Optionally, you could use this excellent/free tool:
http://www.freevbcode.com/ShowCode.Asp?ID=109
0
 

Author Comment

by:CaraFrow
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:CaraFrow
ID: 7019436
Also, what is the story with the shell32.dll file?
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 7021055
<ping><ping>
0
 
LVL 16

Accepted Solution

by:
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
0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7967278
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
VBA/SQL - Connect to SQL server and pull data 4 118
How to compare ms sql hashbytes results within vb6 5 84
Problem to With line 4 63
VB6 - Scroll Mouse wheel on Picturebox 13 71
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 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