Emailing Document with Macros

G

Georgie8888

Hi,

I am trying to use the code below, which I found on this website, but I am
getting an error at " Set objMessage = objSession.Outbox.Messages.Add". This
is about the fourth different code I've tried to have Word email the active
document as an attachment. I've managed (with help) to get it working in
Excel. Why can't they work the same? Any ideas as to my error message?

I am using Windows XP.

Thanks for any help.

Georgie

Sub MapiSendMail()
Dim objSession As Object
Dim objMessage As Object
Dim objRecipient As Object
Dim sProfile As String
Dim sSubjPrmpt As String
Dim sTextPrmpt As String
Dim sEmailPrmpt As String
Dim sMsgTitle As String

sProfile = ""
sEmailPrmpt = "Enter valid Email Name of message recipient:"
sSubjPrmpt = "Enter the subject line for this message:"
sTextPrmpt = "Enter the text for this message:"
sMsgTitle = "Mapi Macro Example"

Set objSession = CreateObject("mapi.session")

objSession.Logon profileName:=sProfile

Set objMessage = objSession.Outbox.Messages.Add

objMessage.Subject = InputBox(sSubjPrmpt, sMsgTitle)
objMessage.Text = InputBox(sTextPrmpt, sMsgTitle)

Set objRecipient = objMessage.Recipients.Add

objRecipient.Name = InputBox(sEmailPrmpt, sMsgTitle)
objRecipient.Resolve

objMessage.Send showDialog:=True
MsgBox "Message sent successfully!"

objSession.Logoff
End Sub
 
G

Georgie8888

Hi Jezebel,

After discussing the issue with a colleague, who's experienced with macros,
we were able to use the one below. I had used this one in Excel and couldn't
understand why it wouldn't translate to Word (with minor tweaks), turns out I
didn't tick the Outlook Library Objects in VBA. This code is much simpler
than the MAPI one. THanks for your help though!

Sub Mail_document_Outlook()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "This is the Subject line" + ActiveDocument.Name
.Body = "Hi there"
.Attachments.Add ActiveDocument.FullName
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

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