Sending an email via Access 2000

N

nata

hi, i'm trying to send an email via access form (email address, content
are entered in access' form) but it juz can't work. i had refered to
Mr.Simon Harvey post (Sending a standard email from Access 2000?

,08-02-03 12:35) and i still not quite get it. Can somebody out there
give me a hand?
 
N

nata

This is the code that i grab from the website,it seems to be operating
(MS outlook runs) but there is no email posted. thank you so much.



Public Function SendEMail()



Dim db As DAO.Database

Dim MailList As DAO.Recordset

Dim MyOutlook As Outlook.Application

Dim MyMail As Outlook.MailItem

Dim Subjectline As String

Dim BodyFile As String

Dim fso As FileSystemObject

Dim MyBody As TextStream

Dim MyBodyText As String



Set fso = New FileSystemObject





Subjectline$ = InputBox$("Please enter the subject line for this
mailing.", _

"We Need A Subject Line!")





If Subjectline$ = "" Then

MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _

"Quitting...", vbCritical, "E-Mail Merger"

Exit Function

End If





BodyFile$ = InputBox$("Please enter the filename of the body of the
message.", _

"We Need A Body!")





If BodyFile$ = "" Then

MsgBox "No body, no message." & vbNewLine & vbNewLine & _

"Quitting...", vbCritical, "I Ain't Got No-Body!"

Exit Function

End If





If fso.FileExists(BodyFile$) = False Then

MsgBox "The body file isn't where you say it is. " & vbNewLine &
vbNewLine & _

"Quitting...", vbCritical, "I Ain't Got No-Body!"

Exit Function

End If



Set MyBody = fso_OpenTextFile(BodyFile, ForReading, False,
TristateUseDefault)



MyBodyText = MyBody.ReadAll



MyBody.Close



Set MyOutlook = New Outlook.Application



Set db = CurrentDb()



Set MailList = db.OpenRecordset("MyEmailAddresses")



Do Until MailList.EOF



Set MyMail = MyOutlook.CreateItem(olMailItem)



MyMail.To = MailList("email")



MyMail.Subject = Subjectline$



MyMail.Body = MyBodyText



MyMail.Send



MailList.MoveNext



Loop



Set MyMail = Nothing



MyOutlook.Quit

Set MyOutlook = Nothing



MailList.Close

Set MailList = Nothing

db.Close

Set db = Nothing



End Function
 
C

Cheryl Fischer

The following code will send an email. I have put comments in the code
explaining the changes I made:

If you are going to work with the FileSystemObject, you might want to
bookmark the following link in MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/ht
ml/sgprogrammingfilesystemobject.asp

If you need further assistance, please post back and either describe the
behavior the code produces, the exact error message, *and* the line of code
which produces the error.


Public Sub SendEMail()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim MyBodyText As String

'Change this:
'Dim fso As FileSystemObject
'To this:
Dim fso As Object

'Change this:
'Dim MyBody As TextStream
'To this:
Dim MyBody As Object

'Add this:
Dim f1 As Object

'Replace this:
'Set fso = New FileSystemObject
'With this:
Set fso = CreateObject("Scripting.FileSystemObject")

' In the code you posted, you were using BodyFile$ = ...
' Since you dimmed a variable as BodyFile, why not use it?
BodyFile = InputBox$("Please enter the filename of the body of the
message.", _
"We Need A Body!")

' Now, you have to actually create the text file
Set f1 = fso.CreateTextFile(BodyFile, True)

If BodyFile = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If

If fso.FileExists(BodyFile) = False Then
MsgBox "The body file isn't where you say it is. " & vbCRLF & _
vbCRLF & "Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If

Subjectline = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")

If Subjectline = "" Then
MsgBox "No subject line, no message." & vbCRLF & vbCRLF & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Sub
End If

' You need to write something to the text file - right?
' Write a line with a newline character.
f1.WriteLine ("Testing 1, 2, 3.")
' Write three newline characters to the file.
f1.WriteBlankLines (3)
' Write a line.
f1.Write ("This is a test.")
f1.Close

Set MyBody = fso_OpenTextFile(BodyFile, , False)

MyBodyText = MyBody.ReadAll
MyBody.Close

' Now, send the email
Set MyOutlook = New Outlook.Application
Set MyMail = MyOutlook.CreateItem(olMailItem)

Set db = CurrentDb
Set MailList = db.OpenRecordset("MyEmailAddresses")

Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.To = MailList("email")
MyMail.Subject = Subjectline
MyMail.Body = MyBodyText
MyMail.Send
MailList.MoveNext
Loop

Set MyMail = Nothing

MyOutlook.Quit

Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing

End Sub
 
N

nata

hi, error again, when i run the macro, it give an error that say "the
expression you entered has a function name that Microsoft Access
can't find."



i entered "RunCode" for the macro action and function name is
=SendEMail() whereby EMail is the field name for the query to store all
the addresses.



thanks a trillion for helping me.THANKS!
 
C

Cheryl Fischer

Try creating a command button on your form and placing the following in the
OnClick event of the command button:

SendEmail
 
C

Cheryl Fischer

To expand a bit ...

Copy all of the code I sent you in my previous post to a public module. If
you would prefer not to overwrite your own code, change the name of the Sub
that I sent you to SendEMail1.

Then, create a command button on your form. In the OnClick event of that
command button, insert the following:

SendEMail1

If you need further assistance, please post back and either describe the
behavior the code produces, the exact error message, *and* the line of code
which produces the error.
 
N

nata

hi,i'm not sure whether this is what u mean but i juz place a runmacro
button in the form n the onclick properties is [Event Procedure]. In
the code, i put something (SendEmail()) as follow, n the error is
Syntax Error



Private Sub sendemail_Click() 'yellow highlighted in this line

SendEmail()



On Error GoTo Err_sendemail_Click



Dim stDocName As String



stDocName = "project"

DoCmd.RunMacro stDocName



Exit_sendemail_Click:

Exit Sub



Err_sendemail_Click:

MsgBox Err.Description

Resume Exit_sendemail_Click



End Sub

'code end



Thanks for the help and sorry for the trouble
 
C

Cheryl Fischer

Please do the following ...

1. Copy all of the code found at the bottom of this posting into a public
Module. The module should *not* be named SendEmail1 . If you do not have
any public modules, create one and save it using a name like basEmail.

2. Now, on your form, create a Command Button. In the properties sheet
for that command button, find the OnClick event and click anywhere on that
line and you will see a downward pointing arrow at the right edge of the
line. Click it and select Event Procedure. Then, look for the small button
to the right of the downward pointing arrow which contains an ellipsis
(three dots ...). Click this button to open the code window. In the code
window, type the following:

SendEMail1

The above line is the only thing you need to have in the code behind the
command button. It will execute the procedure that you have copied to a
public Module. We are not using a macro here. If you use the Wizard to
create your command button, it may put some code in the OnClick event for
you. Delete that code and replace it with the line above: SendEMail1

Do not use "SendEmail1()" or "(SendEMail1())" , as the code is not a
function but a procedure. (Functions return values - procedures do not.
Since the code to send an email does not return a value, I have made it a
procedure.)

3. Re - Naming Command Buttons: I notice that in your most recent post
that you named your command button SendEmail. By default, Access names
command buttons "Command1", "Command2", etc. While I think it is a good
idea to provide a descriptive name to the command button, it is not a good
idea to re-use object names in Access. If you want to rename the command
button for clarity, I'd suggest you rename it so that its object type is
identifiable: cmdSendEmail


COPY THIS CODE INTO YOUR PUBLIC MODULE:

Public Sub SendEMail1()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim MyBodyText As String

'Change this:
'Dim fso As FileSystemObject
'To this:
Dim fso As Object

'Change this:
'Dim MyBody As TextStream
'To this:
Dim MyBody As Object

'Add this:
Dim f1 As Object

'Replace this:
'Set fso = New FileSystemObject
'With this:
Set fso = CreateObject("Scripting.FileSystemObject")

' In the code you posted, you were using BodyFile$ = ...
' Since you dimmed a variable as BodyFile, why not use it?
BodyFile = InputBox$("Please enter the filename of the body of the
message.", _
"We Need A Body!")

' Now, you have to actually create the text file
Set f1 = fso.CreateTextFile(BodyFile, True)

If BodyFile = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If

If fso.FileExists(BodyFile) = False Then
MsgBox "The body file isn't where you say it is. " & vbCRLF & _
vbCRLF & "Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If

Subjectline = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")

If Subjectline = "" Then
MsgBox "No subject line, no message." & vbCRLF & vbCRLF & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Sub
End If

' You need to write something to the text file - right?
' Write a line with a newline character.
f1.WriteLine ("Testing 1, 2, 3.")
' Write three newline characters to the file.
f1.WriteBlankLines (3)
' Write a line.
f1.Write ("This is a test.")
f1.Close

Set MyBody = fso_OpenTextFile(BodyFile, , False)

MyBodyText = MyBody.ReadAll
MyBody.Close

' Now, send the email
Set MyOutlook = New Outlook.Application
Set MyMail = MyOutlook.CreateItem(olMailItem)

Set db = CurrentDb
Set MailList = db.OpenRecordset("MyEmailAddresses")

Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.To = MailList("email")
MyMail.Subject = Subjectline
MyMail.Body = MyBodyText
MyMail.Send
MailList.MoveNext
Loop

Set MyMail = Nothing

MyOutlook.Quit

Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing

End Sub

END OF CODE TO BE COPIED

--
Cheryl Fischer
Law/Sys Associates
Houston, TX

nata said:
hi,i'm not sure whether this is what u mean but i juz place a runmacro
button in the form n the onclick properties is [Event Procedure]. In
the code, i put something (SendEmail()) as follow, n the error is
Syntax Error



Private Sub sendemail_Click() 'yellow highlighted in this line

SendEmail()



On Error GoTo Err_sendemail_Click



Dim stDocName As String



stDocName = "project"

DoCmd.RunMacro stDocName



Exit_sendemail_Click:

Exit Sub



Err_sendemail_Click:

MsgBox Err.Description

Resume Exit_sendemail_Click



End Sub

'code end



Thanks for the help and sorry for the trouble
 
N

nata

hi, i haf modified all those code, no more error.. but there is no email
received by my other account. i mean it's like no email have been sent
out. i wonder what's goes wrong.
 
C

Cheryl Fischer

If you have gotten through the file creation portion of the code without
error, I would check to see if the email message is sitting in Outlook's
"Outbox" file.

Or, post the exact code you are using to send the test email.
 
N

nata

hi, i haf check the outlook express outbox,there's nothing in it. the
code that i haf been used is the 1 that given by you. when i run the
application from access it actually read untill the end of the code
which is msgbox shown. but there's nothing happend. by the way, the
application only prompt to enter FILENAME and SUBJECT LINE after i
entered the email address in the access form. i've try to send mail
using my outlook express and it function well,so it shouldn't be outlook
express problem,right? Really sorry for keep troubling you and thanks.



Public Sub SendEMail()



Dim db As DAO.Database

Dim MailList As DAO.Recordset

Dim MyOutlook As Outlook.Application

Dim MyMail As Outlook.MailItem

Dim Subjectline As String

Dim BodyFile As String

Dim MyBodyText As String



'Change this:

'Dim fso As FileSystemObject

'To this:

Dim fso As Object



'Change this:

'Dim MyBody As TextStream

'To this:

Dim MyBody As Object



'Add this:

Dim f1 As Object



'Replace this:

'Set fso = New FileSystemObject

'With this:

Set fso = CreateObject("Scripting.FileSystemObject")



' In the code you posted, you were using BodyFile$ = ...

' Since you dimmed a variable as BodyFile, why not use it?

BodyFile = InputBox$("Please enter the filename of the body of the
message.", "We Need A Body!")



' Now, you have to actually create the text file

Set f1 = fso.CreateTextFile(BodyFile, True)



If BodyFile = "" Then

MsgBox "No body, no message." & vbNewLine & vbNewLine & _

"Quitting...", vbCritical, "I Ain't Got No-Body!"

Exit Sub

End If



If fso.FileExists(BodyFile) = False Then

MsgBox "The body file isn't where you say it is. " & vbCrLf & _

vbCrLf & "Quitting...", vbCritical, "I Ain't Got No-Body!"

Exit Sub

End If



Subjectline = InputBox$("Please enter the subject line for this
mailing.", _

"We Need A Subject Line!")



If Subjectline = "" Then

MsgBox "No subject line, no message." & vbCrLf & vbCrLf & _

"Quitting...", vbCritical, "E-Mail Merger"

Exit Sub

End If



' You need to write something to the text file - right?

' Write a line with a newline character.

f1.WriteLine ("Testing 1, 2, 3.")

' Write three newline characters to the file.

f1.WriteBlankLines (3)

' Write a line.

f1.Write ("This is a test.")

f1.Close



Set MyBody = fso_OpenTextFile(BodyFile, , False)



MyBodyText = MyBody.ReadAll

MyBody.Close



' Now, send the email

Set MyOutlook = New Outlook.Application

Set MyMail = MyOutlook.CreateItem(olMailItem)



Set db = CurrentDb

Set MailList = db.OpenRecordset("MyEmailAddresses")



Do Until MailList.EOF

Set MyMail = MyOutlook.CreateItem(olMailItem)

MyMail.To = MailList("email")

MyMail.Subject = Subjectline

MyMail.Body = MyBodyText

MyMail.send

MailList.MoveNext

Loop



MsgBox "Email Sent successfully!Thank you!", vbInformation, "Email Sent"

Set MyMail = Nothing



MyOutlook.Quit



Set MyOutlook = Nothing

MailList.Close

Set MailList = Nothing

db.Close

Set db = Nothing



End Sub
 
C

Cheryl Fischer

hi, i haf check the outlook express outbox,there's nothing in it.

The code supplied to you originally as well as the corrected version that I
gave you will only work with Microsoft Outlook, not Outlook Express. So,
any mail item created with this code should be in Microsoft Outlook - not
OE. (To the best of my knowledge, OE does not expose its objects or methods
to coding.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top