Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 8th, 2004, 12:48 PM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Emailing from MS Access using vba and outlook

HI. I greatly appreciate any help or advice in advance. I am currently using the following ms access which sends an email to a specified outlook account. The code also has other functions, but my main concern is adding something to the email objects which will let me specify an account the email is coming from. As it is now, it only shows the sender as the default account. However, it should be sent from a generic account.

Private Sub cmdSendEmail_Click()

Dim strEmailName As String
Dim strEmailAddress As String
Dim strCurrentAttempt As String
Dim sRecipient As String
Dim iPreviousAttempts As Integer
Dim objProp As UserProperty

Dim strBody As String
Dim objOutlook As Outlook.Application
Dim objNameSpace As NameSpace
Dim objFolder As MAPIFolder
Dim objEmail As Outlook.MailItem
Dim rec As Recordset
Dim HistoryRec As Recordset
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Dim BodyFile As String

' start by createthe FileSystemObject
Set fso = New FileSystemObject

' creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objEmail = objOutlook.CreateItem(olMailItem)

' Setup our connection to the database
Set rec = Me.Recordset

' create string with email address
If (IsNull(rec("Email_ID")) = True) Then
    MsgBox "E-Mail/Core_ID is not filled in", vbOKOnly
    GoTo EndOfSub
Else
    strEmailAddress = rec("email_id") & "@company.com"
End If
If (IsNull(rec("username")) = True) Then
    strEmailName = ""
Else
    strEmailName = rec("username")
End If
If (IsNull(rec("Attempt")) = True) Then
    strCurrentAttempt = ""
Else
    strCurrentAttempt = Trim(rec("Attempt"))
End If


' determine the email attempt and fill the body
' with the correct message
If (strCurrentAttempt = "") Then
    ' Setup our FIRST Attempt message
    iPreviousAttempts = 0
    BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile1.txt"
    ' open the file for reading
    Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile1.txt", ForReading, False, TristateUseDefault)
    ' dump the contents of the file into the MyBodyText string
    MyBodyText = MyBody.ReadAll
    ' close the file
    MyBody.Close


ElseIf (Left(strCurrentAttempt, 12) = "1st Attempt ") Then
    ' Setup our SECOND Attempt message
    iPreviousAttempts = 1
    BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile2.txt"
    Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile2.txt", ForReading, False, TristateUseDefault)
    MyBodyText = MyBody.ReadAll
    MyBody.Close

ElseIf (Left(strCurrentAttempt, 12) = "2nd Attempt ") Then
    ' Setup our THIRD/LAST Attempt message
    iPreviousAttempts = 2
    BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile3.txt"
    Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile3.txt", ForReading, False, TristateUseDefault)
    MyBodyText = MyBody.ReadAll
    MyBody.Close

Else
    ' More than three attempts and we just ignore the email
    MsgBox "Three attempts have been made", vbOKOnly
    GoTo EndOfSub
End If

If (BodyFile = "") Then
    MsgBox "Body is empty", vbOKOnly
    GoTo EndOfSub
End If

'creates and sends email
With objEmail
  .To = sRecipient
  .Subject = "Your request for your next computer."
  .Body = MyBodyText
  .Send
End With

EndOfSub:
'Closes outlook. remove you do not want to close outlook
' objOutlook.Quit

Set objOutlook = Nothing
Set objNameSpace = Nothing
Set objFolder = Nothing
Set objEmail = Nothing

' Cause the subform to refresh
Refresh
End Sub

Please let me know if you have any suggestions.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Emailing From MS Access 2000 baNTON Access 5 October 13th, 2007 09:58 AM
Automated Emailing through Outlook donrafeal7 Access VBA 1 February 25th, 2007 12:26 AM
Events with VBA & MS Access hoen VB How-To 0 January 19th, 2006 10:18 PM
MS Access and VBA Error Diahann Access VBA 10 January 7th, 2005 05:46 PM
Calling Outlook Express from Access VBA lmcharl Access VBA 1 September 8th, 2003 11:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.