Thread: [Solved] Email from Command Button
View Single Post
 
Old 05-03-2016, 09:43 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Unfortunately, there is far more wrong with the code than an omission of a reference to Outlook, which in any case is not entirely necessary, depending on how you define the various Outlook objects. At a cursory glance the most obvious problem is that you are trying to paste to the message body after you have sent it?

The following code does not require the reference to Outlook and will paste the document body to the message body.

Unfortunately that too will not achieved the desired aim, because the fields you propose to insert into the message body, to take user input, are not compatible with the message format and so will not work when the user receives the message.

You have two choices:
1. Don't use fields in the document. A two column table with space for the responses in the second column may work for you.
2. Use fields but send the form as an attachment. You don't need the document inspector to do that, unless you want to add a covering message - see the second code example.

In both code examples, the message is displayed momentarily. Do not remove the .Display command or the message body will not be edited.

Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objInspector As Object
Dim objDoc As Word.Document
Dim objRange As Range

    ActiveDocument.Content.Copy
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        MsgBox "Outlook is not running."
        'While you can create an Outlook object in code, " & _
        "the message cannot be edited reliably using the Outlook Inspector when you do so, " & _
        "so it is better to start Outlook first."
        GoTo lbl_Exit
    End If
    On Error GoTo 0

    Set objOutlookMsg = objOutlook.CreateItem(0)
    With objOutlookMsg
        .to = "recipient@domain.com"
        .Subject = "Report"
        Set objInspector = .GetInspector
        Set objDoc = objInspector.WordEditor
        Set objRange = objDoc.Range(0, 0)
        .Display
        objRange.Paste
        .send
    End With
lbl_Exit:
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objOutlookMsg = Nothing
    Set objInspector = Nothing
    Set objOutlook = Nothing
    Exit Sub
End Sub
Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objInspector As Object
Dim objDoc As Word.Document
Dim objRange As Range
Dim sDocname As String
    ActiveDocument.Save
    sDocname = ActiveDocument.FullName
    If Len(ActiveDocument.Path) = 0 Then
        MsgBox "Document is not saved!"
        GoTo lbl_Exit
    End If
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        MsgBox "Outlook is not running."
        'While you can create an Outlook object in code, " & _
         "the message cannot be edited reliably using the Outlook Inspector when you do so, " & _
         "so it is better to start Outlook first."
        GoTo lbl_Exit
    End If
    On Error GoTo 0

    Set objOutlookMsg = objOutlook.CreateItem(0)
    With objOutlookMsg
        .to = "recipient@domain.com"
        .Subject = "Report"
        .attachments.Add sDocname
        Set objInspector = .GetInspector
        Set objDoc = objInspector.WordEditor
        Set objRange = objDoc.Range(0, 0)
        .Display
        objRange.Text = "Complete the attached report and return to the sender."
        .send
    End With
lbl_Exit:
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objOutlookMsg = Nothing
    Set objInspector = Nothing
    Set objOutlook = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote