Using a macro in a word document

Hello All,
I use this bit of code to send an email by pressing a button on a form in
Excel...
Private Sub Button91_Click()
   'Application.Dialogs(xlDialogSendMail).Show
   ActiveWorkbook.SendMail Recipients:="**********@epcor.ca"
   MsgBox "Your meter read was sent", vbInformation, "****Meter Reads"
But When I try to use the same code in a Word document...
Private Sub Button91_Click()

   ActiveDocument.SendMail Recipients:="************@epcor.ca"
   MsgBox "Your meter read was sent", vbInformation, "*****Meter Reads"
 End Sub

I get an error msg. "Wrong number of arguments or invalid property
assignment (Error 450)"

What am I doing wrong??

Thnx,
Sheldon
(BadRasta)
BadRastaManAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JasonRodriguesCommented:
Another way of doing it would be to save the workbook and send it as an attachment this will work help you to select additional options automatically and will work in both Word and Excel

code is given below

'Add a reference to Microsoft Outlook x.0 Object Library
Dim OLF As Outlook.MAPIFolder, olMailItem As Outlook.MailItem
Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olMailItem = OLF.Items.Add ' creates a new e-mail message
   
With olMailItem
        .Subject = "HDFC Bank Status Report" ' message subject
        .Body = "Message Body"
        .To = "Receipient Name"
        .FlagDueBy = Now + 1  'Flag the message
        .FlagRequest = "Reply" 'Set the Flag Type
        .Importance = olImportanceHigh ' Set the importance of the message
        .Attachments.Add Filename 'Filename to attach the message
        .OriginatorDeliveryReportRequested = True ' delivery confirmation
        .ReadReceiptRequested = True ' read confirmation
        .Save ' saves the message for later editing
        .Send 'sends the e-mail message (puts it in the Outbox if you are working off-line)
End With

Set olMailItem = Nothing
Set OLF = Nothing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BadRastaManAuthor Commented:
Hi Jason,
Thank you very much...
2 major things
1. When I cut & past the code given & pres play I get an error message that says "Compile error: Invalid Outside Procedure" I press OK and the word "Set" (the first word of the second line) is highlighted

2. I don't know how to attach this code to a command button on the form
0
JasonRodriguesCommented:
Hi Shelton,

The reason for the error is that you have not put the code in a 'Sub', 'Function' or a 'Event'. You will have to put the code in your Button91 Click Event as below.

Private Sub Button91_Click()
Dim OLF As Outlook.MAPIFolder, olMailItem As Outlook.MailItem
Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olMailItem = OLF.Items.Add ' creates a new e-mail message

    With olMailItem
        .Subject = "Test Mail" ' message subject
        .Body = "This is a test mail to check if the document can be mailed"
        .To = "test@testmail.com"
        .FlagDueBy = Now + 1  'Flag the message
        .FlagRequest = "Reply" 'Set the Flag Type
        .Importance = olImportanceHigh ' Set the importance of the message
        .Attachments.Add (CurDir + "\" + ActiveDocument.Name) 'Open Filename
        .OriginatorDeliveryReportRequested = True ' delivery confirmation
        .ReadReceiptRequested = True ' read confirmation
        .Save ' saves the message for later editing
        .Send 'sends the e-mail message (puts it in the Outbox if you are working off-line)
End With
Set olMailItem = Nothing
Set OLF = Nothing
End Sub
0
JasonRodriguesCommented:
Hi Shelton,

I overlooked your second question of how to attach this code to a command button on a form.

Is it the word doucment itself or a user form. I presume that it is a word document, you could add a command button on the word document right click it and click on View Code. If you do this then ignore the 1st & last line of the code posted above viz.  Private Sub Button91_Click() & End Sub.
0
BadRastaManAuthor Commented:
It took me a while, but this is the most efficient way I found to send an email by pressing a button. The part that was escaping me was saving it to a file first and a friend figured that out for me (thanks Chad)

I hope this helps someone...

Private Sub Submit_Click()
ActiveDocument.SaveAs "C:\whateveryourdocis.doc"
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Set objmail = objol.CreateItem(olMailItem)
    With objmail
        .To = "Insert email address here" 'enter in here the email address
        .CC = "" 'enter in here the email address
        .Subject = "Insert email subject here" ' & dname
        .Body = "" & _
            vbCrLf & "" & vbCrLf
        .NoAging = True
        .Attachments.Add "C:\whateveryourdocis.doc" 'adds attachment to email
        .Display
    End With
    Set objmail = Nothing
    Set objol = Nothing
    SendKeys "%{s}", True 'send the email without prompts
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.