Automatic email from Word

E

Edgar

I have the following code to send out an automatic email
from word but i have a few queries.

1. How can i amend the body of the message
2. How can i set an email address which is part of the
document to a variable so dont have to manually enter the
email address each time.
 
E

Edgar

sorry forgot to insert code:

ub SendDocumentAsAttachment()

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.Subject = "Remittance"
'Add the document as an attachment, you can use
the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName,
Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With

If bStarted Then
oOutlookApp.Quit
End If

Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub
 
M

Michael Bednarek

I have the following code to send out an automatic email
from word but i have a few queries.

1. How can i amend the body of the message
2. How can i set an email address which is part of the
document to a variable so dont have to manually enter the
email address each time.

With oItem
.Body = "Hi Edgar," & vbCR & vbCR
.Body = .Body & "This is how to populate the message body."
End With

You may have to experiment if the .Body is RTF or HTML. I also believe
that Outlook 10/11 has additional properties, like .BodyPlainText or
somesuch; I have only access to Outlook 9 at this moment.

As for the e-mail address: just assign the range of the Word document
which contains the address to a string variable. One way would be to
create a Bookmark, say "Address", over the address string, then in VBA:
theAddress = Activedocument.Bookmarks("Address").Range
 
H

Helmut Weber

Hi Edgar,
Michael's advice is for the more advanced users.
If you can code using ranges, it sure is preferably.
....and stay in the original thread.
Makes following the discussion much easier.
Have a nice day.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
 

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