Solved

Send mail from VB application Using Outlook 2000

Posted on 2002-06-20
4
159 Views
Last Modified: 2010-05-02
Dear experts,

I know it is very simple to do but somehow i could not do it. poor guy. Please help me to write that function how to use Outlook object and send mails automatically. I tried to do it, after seting the recepeint, suject and body, VB was indicating some error on send command. some amboguitiy.. .. no idea la...

Thanking you for your prompt support....

waiting.....
0
Comment
Question by:mamaj
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
gencross earned 100 total points
ID: 7096603
Here is some code that will open Outlook and mail with attachments

Sub Email()
   
   Dim objOL As Object
   Dim objEMail As Object
   
   Set objOL = CreateObject("Outlook.Application") 'opens Outlook
   
   Set objEMail = objOL.CreateItem(olMailItem) ' opens new email
   With objEMail
       .Recipients.Add "me@me.com" 'recipient's email address
       .Subject = "Your details " & x 'subject of the email
       .Body = "Details"
       'To send an attachment
       .Attachments.Add ("C:\my documents\file.txt")
       .Send
   End With
   
   Set objEMail = Nothing
       
End Sub

This should do it if I did not forget anything.
0
 
LVL 5

Expert Comment

by:rpai
ID: 7097064
You could use the following lines of code to send an email using Outlook 2000
-------------------Start Code------------------------------
Public Function SendMessage() As Boolean

On Error Resume Next

Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String

strSubject = "Test Email"
strRecip = "test@test.com"
strMsg = "Test Email using Outlook Application"
'strAttachment = "C:\test.txt"

'-- verify that the user supplied an Email address for a recipient.
If Len(strRecip) = 0 Then
    strMsg = "You must designate a recipient."
    MsgBox strMsg, vbExclamation, "Error"
    Exit Function
End If

'-- Success
fSuccess = True

'-- The Outlook Automation
If GetOutlook = True Then
    Set mItem = mOutlookApp.CreateItem(olMailItem)
    mItem.Recipients.Add strRecip
    mItem.Subject = strSubject
    mItem.Body = strMsg
   
'-- Allows 1 attachment, but with slight modification, you could provide for multiple files.
    If Len(strAttachment) > 0 Then
        mItem.Attachments.Add strAttachment
    End If
   
    mItem.Save
    mItem.Send
End If
----------------------End Code-----------------------------
' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False
SendMessage = fSuccess

End Function

Private Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
    Err.Clear
    Set mOutlookApp = CreateObject("Outlook.application")
       
    If Err.Number > 0 Then
        MsgBox "Could not create Outlook object", vbCritical
        fSuccess = False
        Exit Function
    End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
   
If Err.Number > 0 Then
    MsgBox "Could not create NameSpace object", vbCritical
    fSuccess = False
    Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
   
End Function
0
 
LVL 5

Expert Comment

by:rpai
ID: 7097067
Ooops..!
The ---------End Code ----------- came up pre-maturely in my above post!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7097089
I prefer a lot using vbSendMail. Very easy to use. Free. Does not have requirements.

http://www.freevbcode.com/ShowCode.Asp?ID=109
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

808 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