Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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)
0
BadRastaMan
Asked:
BadRastaMan
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now