Discussion Locked

Auto-E-mailling using VB in Word 2000

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

Could anyone tell me how to send a Word 2000 document as an attachment to a fixed e-mail address (with a fixed comment/message), using VB. I am sending the e-mail via Outlook 2000, so do I need special Libraries installed (I have a standard Office 2000 Pro installation).

Thanks,

Ben.

4 replies

by awilliams3

The sample attaches the document containing the code so in effect it will send the file in its last 'SAVED' state. So it would be wise to save the document prior to executing the code. This can implemented in code as well.

To set up this sampleyou will need a new Word document.

In the Word document use the ALT+F11 key combination to switch to the Visual Basic Editor (VBE).

In VBE, insert a module into the new document.

Select TOOLS/REFERENCES from the main VBE menu.

Find the Microsoft Outlook library and check it (use the latest version if more than one is present).

Select OK.

Insert the following code into the module.

Public Sub createEmailDocWithAttachmentSample()
Dim olObj As Outlook.Application
Dim mlItem As Outlook.MailItem

getoutlook oleToOutlook:=olObj

If olObj Is Nothing Then
MsgBox prompt:="Unable to find Outlook!", _
buttons:=vbExclamation + vbOKOnly, _
Title:=ThisDocument.FullName
Exit Sub
End If

Set mlItem = olObj.CreateItem(itemtype:=MailItem)
With mlItem
.Display
.To = "test@test.com"
.Subject = "this is a test"
.Body = "Basic text"
.Attachments.Add Source:=ThisDocument.FullName
End With

Set mlItem = Nothing
Set olObj = Nothing
End Sub

Private Function getoutlook(oleToOutlook As Outlook.Application)
Set oleToOutlook = Nothing

On Error Resume Next
Set oleToOutlook = GetObject(, "Outlook.application")
On Error GoTo 0

If oleToOutlook Is Nothing Then
On Error Resume Next
Set oleToOutlook = CreateObject("Outlook.Application")
On Error GoTo 0
End If

End Function

Save the new document.

Execute the createEmailDocWithAttachmentSample routine to try it.

by bmw

Note for awilliams ..... How can I give you the points for this question? I intended to, but I accidentally rejected your answer! Sorry.

This discussion is closed to new replies.