Solved

Sending automatic emails with Visual Basic (in Excel)

Posted on 2002-05-17
8
478 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
 

Author Comment

by:CaraFrow
ID: 7019436
Also, what is the story with the shell32.dll file?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now