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

Send mail from VB application Using Outlook 2000

Posted on 2002-06-20
4
168 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

856 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