none
Use form field data in email subject/body RRS feed

  • Question

  • I have created a form using Word. The form has a Command Button that sends the form as an attachment. I would like to use data from form fields in the subject of the email.

    The form is used to request an appointment and I'd like for the user's name and the date of their appointment to show up in the subject line to look something like "Appointment for John Doe on 02/30/2018". 

    Is this possible and if so can someone show me what the code would look like for it?

    Thanks in advance


    • Edited by Brian8541 Sunday, April 15, 2018 2:51 AM
    • Moved by Perry-Pan Monday, April 16, 2018 5:54 AM
    Sunday, April 15, 2018 2:50 AM

All replies

  • Hi Brian,

    Thank you for visiting Outlook Forum.

    Then here we mainly focus on  general questions and feedback related to Office 2010 desktop application. Since your issue is more related to form code, I'll help move this thread to Word for Developer forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, 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.

    Regards,

    Perry


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, April 16, 2018 5:52 AM
  • Hello Brian8541,

    Here is a simply code to create an appointment and set the subject. Please try to adjust it for your need.

    Sub Test()
        'get username from your form
        UserNameStr = "TestUserName"
        'get date from your form
        DateStr = Format(Now, "mm/dd/yyyy")
        
        'create outlook application instance
        Set OutlookApp = CreateObject("Outlook.Application")
        'create outlook appointment
        Set appItem = OutlookApp.CreateItem(1)
        'set subject
        appItem.Subject = "Appointment for " & UserNameStr & " on " & DateStr
        'display the appointment for checking
        appItem.Display
        'save the appointment
        appItem.Save
    End Sub
    
    

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 17, 2018 5:52 AM
  • Ok. I inserted you code into the rest of the code I had that worked fine. The result was TestUserName ended up on the subject line instead of what was entered into the form field entitled TestUserName.

    Also, the date comes from a datepicker form field. That doesn't look like it's going to work with your code.

    Any suggestions?

    Tuesday, April 24, 2018 2:36 AM
  • Hello Brian8541,

    >>The result was TestUserName ended up on the subject line instead of what was entered into the form field entitled TestUserName.

    What do you mean this? Would you mind share some screenshots to illustrate the desired result or behavior? 

    If you failed to share the screenshots directly, you could share it via Cloud Storage, such as One Drive, and then share the address here.

    Thanks for understanding,

    Best Regards,

    Terry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 24, 2018 2:46 AM
  • Here is the code as it is now and it works.

                   

    Private Sub Submit_Click()
    Dim OL              As Object
        Dim EmailItem       As Object
        Dim Doc             As Document
        Dim strFileName     As String

        Application.ScreenUpdating = False
        Set OL = CreateObject("Outlook.Application")
        Set EmailItem = OL.CreateItem(0) ' 0 = olMailItem
        Set Doc = ActiveDocument
        strFileName = Replace(Doc.FullName, ".docm", ".pdf")
        Doc.ExportAsFixedFormat OutputFileName:=strFileName, _
            ExportFormat:=wdExportFormatPDF

        With EmailItem

        .Subject = "Appointment for "

        .Body = "Please, find the attached appointment request for "

        .To = "email.address@mymail.com"

        .Importance = olImportanceNormal

        .Attachments.Add strFileName

        .Send

        End With

        Application.ScreenUpdating = True

        Set Doc = Nothing
        Set EmailItem = Nothing
        Set OL = Nothing
        MsgBox "This form has been sent"

        End Sub

    On the form the user fills out is a datepicker field ("Date") and a text field ("Name")

    The result I am looking for is a subject line that reads "Appointment for John Doe on 02/29/2020" when John Doe is entered into Name, and 02/29/2020 entered in the date picker

    And a message body that reads "Please, find the attached appointment request for John Doe on 02/29/2020." with the name and date pulled from the same fields. 

    Tuesday, April 24, 2018 2:56 AM
  • Hello Brian8541,

    In my preview code, I hard-coding the user name and date time. You could also try to get them from the form controls. Here is the example.

    'get user name from the textbox
    'TextBox5 is the name of the textbox control
    UserNameStr = Me.TextBox5.Text
    'get date from the date picker
    'DTPicker1 is the name of the date picker control
    DateStr = Format(Me.DTPicker1.Value, "mm/dd/yyyy")

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 24, 2018 3:18 AM
  • I got this error:

         Compile error:

         Method or data member not found

    I double checked and the name of the text box is the same as the name in the code.

    Tuesday, April 24, 2018 3:31 AM
  • Hello Brian8541,

    Could you please share a screenshot of the form and the properties windows of the text box? 

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 24, 2018 4:01 AM
  • Unfortunately, no. The ability to take screenshots has been disabled for security reasons. I'm happy to tell you how things are set up if you have a specific question.

    I do appreciate the help.

    Tuesday, April 24, 2018 11:02 AM
  • Hello Brian8541,

    Which line do you get error? Please check if my test document could work for you, you could download the file from

    https://1drv.ms/w/s!ArC0gnwxLv5qhxHfy1kJ7hIAJVZ3

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 25, 2018 8:43 AM