• Send mail from Word

    Author
    Topic
    #352782

    Using VBA, I want to mail the current Word document as an attachment. The following code works, but how can you put in the name of the recipient. It’s always the same person and I don’t want to create a special Outlook form for this.

    Options.SendMailAttach = True
    ActiveDocument.SendMail

    Any help out there??
    Gwenda

    Viewing 1 reply thread
    Author
    Replies
    • #514198

      The answer was in “Word 2000 VBA Programmer’s Reference” by Duncan Mackenzie. Word’s sendmail object does not allow you to set any properties, like a recipient’s email address, body text, etc. You have to add the Outlook library to your project References (TOOOLS, References). Then you have access to Outlook’s own beautiful object model. The actual code follows:

      Sub MailProposal(sRecipient As String, strFullFileName As String)

      Dim objOutlook As Outlook.Application
      Dim nsMAPI As Outlook.NameSpace
      Dim objOutbox As Outlook.MAPIFolder
      Dim objNewMessage As Outlook.MailItem

      Set objOutlook = New Outlook.Application
      Set nsMAPI = objOutlook.GetNamespace(“MAPI”)
      Set objOutbox = nsMAPI.GetDefaultFolder(olFolderInbox)
      Set objNewMessage = objOutbox.Items.Add

      With objNewMessage
      .To = sRecipient
      .Subject = “test”
      .Body = “”
      .Attachments.Add strFullFileName .Send
      End With

      Set objOutlook = Nothing
      Set nsMAPI = Nothing
      Set objOutbox = Nothing
      Set objNewMessage = Nothing
      Exit Sub
      End Sub

      • #514214

        Sorry we couldn’t help you. But thanks for putting up the answer. I’ve learnt something, and it will be usefule to anybody else who comes along.

      • #520640

        Gellwood,

        Thanks for posting this code. I found it useful too.

    • #514314

      Thanks Sandi, I would love to know how to program Exchange, but I don’t
      G

      • #570583

        This was code I was looking for to complete a project. I do have one additional question. For the .Attachments.Add line, you are getting a Filename from the user (strFullFileName) but how can I just attach the current document that’s on screen?

        Any help would be appreciated. Thank you.

        • #570753

          You need to give that variable a value somewhere in the macro before it is called
          strFullFileName = ActiveDocument.FullName

          You may also need to make sure the document is saved before attaching it. Otherwise you will get the saved version which may not be the same as the loaded (screen) version. The code for that is
          ActiveDocument.Save

    Viewing 1 reply thread
    Reply To: Send mail from Word

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: