Solved

Sending automatic emails with Visual Basic (in Excel)

Posted on 2002-05-17
8
482 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Build VS2015 project in bat command 3 80
MsgBox 2 48
Notepad++ how to remove delimiter : from beggning of the line? 3 94
Using MS Code on my Mac 6 66
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now