Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Outlook e-mail with VBA

Status
Not open for further replies.

MDJ52

MIS
Mar 30, 2001
120
US
I have a module that is part of an ACCESS database. The module sends out approximately 500 e-mails a day based on criteria. The code is set to loop through a table that contains request information and the customers email address. I set up Outlook , and messages, recipients, and attachments and all objects. In the message I but a few lines about the request I am creating and some return address email and fax info.
The main request is a single page that is output as a document and then attached to the email.
My problem is, many of the customers are now coming back sayint they would prefer faxes because of the virus issues possible in attachments. We assure them that all things going out are scanned for viruses, but many times this does not satisfy the customer.
Is there a way to insert the document into the body of the e-mail. I use the CreateItem(0) method to create the output for the email. I can find nothing in the documentation for Item.body that allows such a process.
Has anyone tried this?
Thanks for your help.
 
MDJ52,
I have tried this and think this will work for you if I understand you question. You may need to do a little tweeking, though.

Dim Olook As Object 'outlook.application
Dim Mitem As Object 'outlook.mailitem
Dim Mitem2 As Object
Dim fname As String

Set Olook = CreateObject("Outlook.Application")
Set Mitem = Olook.CreateItem(0)

Used for an address to an attachment
fname = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name

Mitem.Recipients.Add cboDrafter.Text
Mitem.Recipients.ResolveAll
Mitem.CC = user.mstrRealName
Mitem.Subject = "ECN: " & txtECN.Text
Mitem.Body = "ECN: " & txtECN.Text & " was close for you by " & user.mstrRealName & "." & _
vbCrLf & "If you are closing a package that is all microstation (not encapsulated) drawings, " & _
"you will need to get " & txtECN.Text & ".txt from /users/" & _
user.mstrUserName & "/plots"
Used to add an attachment
Mitem.Attachments.Add fname
Mitem.send
Set Olook = Nothing
Set Mitem = Nothing

I have set the body to text in this case but do not see why it would not be possible to set it to a file. See what you think.

Dugger
 
Idono,
What you have shown here is pretty much what I have been doing. Put some text into the body and then add the attachment.
Is ther a way in that body statement to point to a file on my hard drive, as in yours:
MITEM.Body="c:\my documents\testsend.doc" All this will do is add the statement in quotes to the body.
I wish to have my document as the body of the message rather than the attachment.
 
MDJ52,
I see what you are talking about now and I have rewritten it. It worked for me, so try it and let me know what you think.

Dim Olook As Object 'outlook.application
Dim Mitem As Object 'outlook.mailitem
Dim strFile As String 'File where body text is located
Dim strData As String 'String to hold the body text
Dim FileHandle As Integer

'File where body text is located
strFile = "U:\plots\" & frmMain.txtECN.Text & ".txt"

FileHandle = FreeFile()
Open strFile For Input As #FileHandle
Line Input #FileHandle, strData
Close #FileHandle
Set Olook = CreateObject("Outlook.Application")
Set Mitem = Olook.CreateItem(0)

Mitem.Recipients.Add cboDrafter.Text
Mitem.Recipients.ResolveAll
Mitem.Subject = "ECN: " & txtECN.Text

Mitem.Body = strData 'sets the body equal to the text in a file
Mitem.Send

Set Olook = Nothing
Set Mitem = Nothing

Dugger
 
An alternative to this to use an Outlook Mail Template. Create a new Message Template with your text in the body part of the template, this allows you to be more creative with the formatting of the document.

This is the jist of the code i have used where strOFT is the full file path of Message Template.

Set appOlApp = CreateObject("Outlook.Application")
Set objExt = appOlApp.CreateItemFromTemplate(strOFT)

objExt.To = .Fields(intHEmail - 1)
objExt.SentOnBehalfOfName = strSOBO
objExt.Subject = strSubject
objExt.Send

Thanks

CID8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top