Solved

Send mail from VB application Using Outlook 2000

Posted on 2002-06-20
4
139 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ooops..!
The ---------End Code ----------- came up pre-maturely in my above post!
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

771 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

14 Experts available now in Live!

Get 1:1 Help Now