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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Mails from MS Word

Status
Not open for further replies.

nateshk

Programmer
Jan 16, 2001
49
HK
Hi All,

I need to send a mail from Word document using a macro. The mail will contain some message and additional the document itself will have to be sent as attachment.

I need to send mail to different persons seperately. Say Email1 to person A, Email2 to person2 etc.. The content of the mail will remain same, only the send to address will differ.

Any Help ?????

Thanx in advance

Natesh
 
Do you use Outlook as your mail program? If so, the solution is fairly straightforward.
Rob
[flowerface]
 
Hi Rob,

Yes i have got Outlook as my mailing sw, can u tell me how to move ahead.

Thanx

Natesh
 
I'll copy my (Excel-based) standard procedure for sending e-mails through VBA down here. Before you use the code, set a reference in VBA to the Microsoft Outlook Object Model (using Tools-References). With this sub, you can now send an e-mail, e.g.

CreateMail(array("me@mydomain.com","you@yourdomain.com"),"subject matters", _
"I thought you'd want to see this file:",activedocument.fullname)

Function CreateMail(SendTo As Variant, Subject As String, Message As String, _
AttachmentName As String) As Boolean

Dim ol As Outlook.Application
Dim ns As Outlook.NameSpace
Dim mi As Outlook.MailItem
Dim recip As Variant, objRecip1 As Outlook.Recipient

On Error GoTo CreateMail_Err
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set mi = ol.CreateItem(olMailItem)
With mi
For Each recip In SendTo
Set objRecip1 = .Recipients.Add(recip)
objRecip1.Resolve
If Not objRecip1.Resolved Then
If MsgBox("Could not resolve recipient " & recip & ". Send anyway?", vbYesNo) = vbYes Then
objRecip1.Delete
Else
CreateMail = False
GoTo CreateMail_End
End If
End If
Next recip
If AttachmentName <> &quot;&quot; Then
.Attachments.Add AttachmentName, , , Mid(AttachmentName, InStrRev(AttachmentName, &quot;\&quot;) + 1)
End If
.Subject = Subject
.Body = Message
.Recipients.ResolveAll
.Send
End With
CreateMail = True
CreateMail_End:
Set mi = Nothing
Set ns = Nothing
Set ol = Nothing
Exit Function
CreateMail_Err:
CreateMail = False
Resume CreateMail_End
End Function


Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top