none
Word Macro Content export into Email

    Question

  • Is it possible to create a Macro ,  to export certain data in a Word document into the body of an email ?

    Example below;   I would like to take the content from a work document,  "Date and Time reported",   "IT ticket #", and "Incident level",   create a button in the word doc, so when you click on it,   it takes that data, Exports that into the body of an email,   along with the attached word doc,  as shown below. 


    Thank you for your help

    Monday, October 28, 2013 6:47 PM

Answers

  • Hi,

    According to your description, you want to create an email item and set the email body with the document content and add the Word document with attach file.

    I wrote a sample for your reference:

    Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = "John.Doe@Domain.com"
        .Subject = "Security Incident Report"
        .Body = ActiveDocument.Content
        .Attachments.Add ActiveDocument.FullName
        .Send
    End With
    On Error GoTo 0
    Set OutApp = Nothing
    Set OutMail = Nothing
    End Sub
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Semperfi4000 Thursday, October 31, 2013 12:30 PM
    Thursday, October 31, 2013 3:46 AM

All replies

  • Programming/Code related questions should really be posed in one of the following forums.

    Word for Developers
    http://social.msdn.microsoft.com/Forums/en-US/outlookdev

    I've moved it there and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Cheers,
    Tony Chen
    Forum Support
    ________________________________________
    Come back and mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback on our support, please contact tnmff@microsoft.com.

    Tuesday, October 29, 2013 9:19 AM
  • Hi,

    According to your description, you want to create an email item and set the email body with the document content and add the Word document with attach file.

    I wrote a sample for your reference:

    Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = "John.Doe@Domain.com"
        .Subject = "Security Incident Report"
        .Body = ActiveDocument.Content
        .Attachments.Add ActiveDocument.FullName
        .Send
    End With
    On Error GoTo 0
    Set OutApp = Nothing
    Set OutMail = Nothing
    End Sub
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Semperfi4000 Thursday, October 31, 2013 12:30 PM
    Thursday, October 31, 2013 3:46 AM
  • thank you for your reply;

    I was able to find a solution,   I customized the code, I found here;

    http://answers.microsoft.com/en-us/office/forum/office_2010-word/send-as-pdf/3f312753-e958-41ec-922f-b6812e4704f2?rtAction=1383221370062

    I am not a experienced programmer, so I apologize for any slop programming that I may have added.

    What I need to happen was:

    I needed to do the following;
    1) Create a Control click Button in a word document
    2) when user clicks the button
    • Prompts to Save the file, so you can change the name
    • Your Code:  The file is saved as a PDF, and attaches to the email.
    • An embedded images is placed in the email.
    • Using "ContentControls" in the word document,  I then take certain contentcontrols and place that within the body of an email in an HTML format.


    Private Sub CommandButton1_Click()

        Dim outl As Object
        Dim Mail As Object
        Dim Msg, Style, Title, Help, Ctxt, Response, MyString
        Dim PDFname As String
        
        Dim strRptDate As String
        Dim strDetDate As String
        Dim strITTicket As String
        Dim strPrivacyID As String
        Dim strPolicyRef As String
        Dim strName As String
        Dim strLocation As String
        Dim strTitle As String
        Dim strContact As String
        Dim strManager As String
        Dim strDetails As String

        strRptDate = ActiveDocument.ContentControls(1).Range.Text
        strDetDate = ActiveDocument.ContentControls(2).Range.Text
        strITTicket = ActiveDocument.ContentControls(5).Range.Text
        strPrivacyID = ActiveDocument.ContentControls(8).Range.Text
        strPolicyRef = ActiveDocument.ContentControls(11).Range.Text
        strName = ActiveDocument.ContentControls(17).Range.Text
        strLocation = ActiveDocument.ContentControls(18).Range.Text
        strTitle = ActiveDocument.ContentControls(19).Range.Text
        strContact = ActiveDocument.ContentControls(20).Range.Text
        strManager = ActiveDocument.ContentControls(21).Range.Text
        strDetails = ActiveDocument.ContentControls(31).Range.Text
        

        
        Application.Dialogs(wdDialogFileSaveAs).Show
        
        
        Msg = "Your document will be aved as PDF File. If there is a file with the same name, it will be overwritten, please agree?"
            Style = vbOKCancel + vQuestion + vbDefaultButton2
            Title = "Security Incident Report"
            Ctxt = 1000
            Response = MsgBox(Msg, Style, Title, Help, Ctxt)
            If Response = vbOK Then
                
                ActiveDocument.Save
                PDFname = ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf"
                ActiveDocument.ExportAsFixedFormat OutputFileName:=PDFname, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:=True, UseISO19005_1:=False
                Set outl = CreateObject("Outlook.Application")
                Set Mail = outl.CreateItem(0)
                Mail.Subject = ActiveDocument.Name
                Mail.BodyFormat = olFormatHTML
                Mail.HTMLBody = "<img src='\\FileShare\Documents\ITNotice.gif'> <br>" & _
                "<font face='Calibri' size='3'  color='#000000'> <b>Attached Report:</b>  Detailed information on Security Incident <br><br>" & _
                "<font face='Calibri' size='6'  color='#0082d1'>Brief Summary <br>" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Report Date:</b> &nbsp;" & strRptDate & "&nbsp; &nbsp; &nbsp;" & _
                "<font face='Calibri' size='3'  color='#000000'><b>IT Ticket #:</b> &nbsp;" & strITTicket & "&nbsp; &nbsp; &nbsp;" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Privacy ID #:</b> &nbsp;" & strPrivacyID & "&nbsp; &nbsp; &nbsp;" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Policy Ref:</b> &nbsp;" & strPolicyRef & "&nbsp; &nbsp; &nbsp;<br><br>" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Name:</b> &nbsp;" & strName & "&nbsp; &nbsp; &nbsp;<br>" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Title:</b> &nbsp;" & strTitle & "&nbsp; &nbsp; &nbsp;<br>" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Manager:</b> &nbsp;" & strManager & "&nbsp; &nbsp; &nbsp;<br><br>" & _
                "<font face='Calibri' size='3'  color='#000000'><b>Details of Incident:</b> &nbsp;" & strDetails & "&nbsp; &nbsp; &nbsp;"
                Mail.To = ""
                Mail.Attachments.Add PDFname
                Mail.Display
            Else
                MsgBox "You have chosen not to save your document will not be sent"
                Cancel = True
            End If


    End Sub

    Thursday, October 31, 2013 12:30 PM