PDA

View Full Version : Solved: Attach Word File to Email



Tricia
06-16-2004, 01:23 PM
I am looking for code to attach word files to emails and send them to different people based on a table in access. Any one have any ideas?;)

Anne Troy
06-16-2004, 01:26 PM
Hi, Tricia. :)

So....will it always be the same Word file? Or is the name/path of the file in a field somewhere? Where is the email address? I assume a field, and the coders will need to know that field's name too.

If possible, can you make a copy of your database, remove anything not related, change the names to protect the innocent, compact, zip, and ship up here?

Tricia
06-16-2004, 01:37 PM
I have attached the database. the files will be the check number .rtf and the email adress associated with that file is listed as well in the table numberandemail.

___
06-17-2004, 01:06 AM
I have attached the database. the files will be the check number .rtf and the email adress associated with that file is listed as well in the table numberandemail. Will all the .rtf files be in the same folder? Are the files really named 1.rtf, 2.rtf etc,etc? Anyway, I've created a new form in your db called frmMail. The code to send the email is as below...

Private Sub CmdSendMail_Click()
On Error GoTo Err_CmdSendMail_Click
Dim EmailApp, NameSpace, EmailSend As Object

Set EmailApp = CreateObject("Outlook.Application")
Set NameSpace = EmailApp.GetNamespace("MAPI")
Set EmailSend = EmailApp.CreateItem(0)

EmailSend.To = Me.Mail_Address_S
EmailSend.Subject = "Latest Document"
EmailSend.Body = "Please find attached the latest Word document" & vbCrLf & vbCrLf & "Best Regards" & vbCrLf & "Tricia"
EmailSend.Attachments.Add "C:\" & Me.CheckNumber & ".rtf"
EmailSend.Display

Set EmailApp = Nothing
Set NameSpace = Nothing
Set EmailSend = Nothing

Exit_CmdSendMail_Click:
Exit Sub

Err_CmdSendMail_Click:
If Err Then
MsgBox "The file was not found"
Resume Exit_CmdSendMail_Click
Else
End If

End Sub

you will need to change this line...

EmailSend.Attachments.Add "C:\" & Me.CheckNumber & ".rtf"

to the folder where you store the files, ie.

EmailSend.Attachments.Add "C:\MyDocuments\LatestDocs\" & Me.CheckNumber & ".rtf"

If you need anymore help let me know.

Tricia
06-17-2004, 06:58 AM
That works thanks but, I was looking for a little more automated version. I want it to loop through the whole table with one push of the button and send the e-mails out with out pushing send each time. Any suggestions?

mark007
06-17-2004, 07:02 AM
To send it straight away rather than display it just change:

EmailSend.Display

to

EmailSend.Send

:)

___
06-17-2004, 08:58 AM
See if this works. I've not had time to test it because I've got to run to see the football! :wot

Let me know.
If it doesn't I'll look closer at it tomorrow.

Anne Troy
06-17-2004, 09:09 AM
Okay, guys. That's 2 down to go watch football (which is really soccer), but let us know how the game goes...

___
06-17-2004, 10:56 AM
let us know how the game goes... Eng 3 - Sui 0 :drunkard:*hic*

___
06-18-2004, 12:44 AM
Ok, I've tested it now and this version works. I created a listbox called LstEMail with the Row Source query as below...

SELECT numberandemail.CheckNumber, numberandemail.[Mail Address S]
FROM numberandemail;

then looped through each record in the list...


Private Sub CmdSendToList_Click()
On Error GoTo Err_CmdSendToList_Click

Dim intCurrRow As Integer
Dim EmailApp, NameSpace, EmailSend As Object

For intCurrRow = 0 To LstEMail.ListCount - 1

Set EmailApp = CreateObject("Outlook.Application")
Set NameSpace = EmailApp.GetNamespace("MAPI")
Set EmailSend = EmailApp.CreateItem(0)

EmailSend.To = Me.LstEMail.Column(1, intCurrRow)
EmailSend.Subject = "Latest Document"
EmailSend.Body = "Please find attached the latest Word document" & vbCrLf & vbCrLf & "Best Regards" & vbCrLf & "Tricia"
EmailSend.Attachments.Add "C:\" & Me.LstEMail.Column(0, intCurrRow) & ".rtf"
EmailSend.Display

Next intCurrRow

Set EmailApp = Nothing
Set NameSpace = Nothing
Set EmailSend = Nothing

Exit_CmdSendToList_Click:
Exit Sub

Err_CmdSendToList_Click:
Resume Next

End Sub


and as Mark007 said in his earlier post, to send it straight away rather than display it just change:

EmailSend.Display

to

EmailSend.Send

Hope it's what you wanted.

___
06-18-2004, 07:35 AM
Did it work as you wanted?

Tricia
06-18-2004, 09:21 AM
It Works!!! Case Closed. Thank you!