title: Outlook VBA Script Moving Text From Custom Form to Message Body
body: <p>I have never used Outlook VBA (2010), but my manager tasked me to create a user form that sends IT requests and IS requests. I have the custom form created, where I gather all my text fields and print the text to a single text box. </p>
  
  <p>This action is all defined within <code>Sub CommandButton1_Click()</code>, which ends with <code>Send()</code>.  Within the sub, I have something like this for all text boxes: </p>
  
  <pre><code> Set Sj =Item.GetInspector.ModifiedFormPages("P.2").Controls("Subject_Text")
   Set YNbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("YNBox")
   Set Rbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("ReasonBox")
  </code></pre>
  
  <p>What I want to know is how do I take the texts within "P.2" and paste it into the Message area of "Message" Page?</p>
  
  <p>Here is the code snippet for the printing out to a single text box:</p>
  
  <pre><code>FinalBox.Text = "Subject: " &amp; Sj.Text &amp; vbCrLf &amp; _
  vbCrLf &amp; "Can work around the issue?: " &amp; YNbox.Text &amp; _
  vbCrLf &amp; "Reason For Ticketing: " &amp; Rbox.Text &amp; _
  vbCrLf &amp; "Department: " &amp; Dbox.Text &amp; _
  vbCrLf &amp; "Impact: " &amp; Ibox.Text &amp; _
  vbCrLf &amp; "Urgency: " &amp; Ubox.Text &amp; _
  vbCrLf &amp; "System/Machine Number: " &amp; Mbox.Text &amp; _
  vbCrLf &amp; "Was trying to accomplish: " &amp; Abox.Text &amp; _
  vbCrLf &amp; "Has it occured before?: " &amp; Bbox.Text &amp; _
  vbCrLf &amp; "First Noticed: " &amp; Tbox.Text &amp; _
  vbCrLf &amp; "Others affected by the issue: " &amp; Affbox.Text &amp; _
  vbCrLf &amp; "Additonal Comments: " &amp; Addbox.Text
  </code></pre>
  
  <p>So, how do I take this and append it to the actual message field in Message page?</p>
  
  <p>.</p>
  
  <p>Thank you very much!!</p>
  
  <p>P.S. I've been having issues with <code>MailItem.body</code> and whenever I create an object, for example: </p>
  
  <p><code>Dim objMsg As Object</code>, </p>
  
  <p>I get an error that says "Expected end of statement"... I understand VB and VBA is different, but I didn't think it'd be giving me this much headache.</p>
  
  <p>Edit:</p>
  
  <p>Hello dbMitch and Tony Dallimore, thanks for helping me clarify my questions. Like I mentioned, I am a mere beginner when it comes to VBA, and I Just wanted to </p>
  
  <pre><code>`Sub Commandbutton
   Set Sj = Item.GetInspector.ModifiedFormPages("P.2").Controls("Subject_Text")
   Set YNbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("YNBox")
   Set Rbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("ReasonBox")
   Set Dbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("DepartmentDropbox")
   Set Mbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("MachineBox")
   Set Ibox = Item.GetInspector.ModifiedFormPages("P.2").Controls("ImpactBox")
   Set Ubox = Item.GetInspector.ModifiedFormPages("P.2").Controls("UrgencyBox")
   Set Abox = Item.GetInspector.ModifiedFormPages("P.2").Controls("AccomplishBox")
   Set Bbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("BeforeText")
   Set Tbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("Timebox")
   Set Affbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("AffectedBox")
   Set Addbox = Item.GetInspector.ModifiedFormPages("P.2").Controls("AdditionalBox")
   Set Tbox8 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox8")
   Set MESBOX = Item.GetInspector.ModifiedFormPages("Message").Controls("Message")
  
   Tbox8.Text = "Subject: " &amp; Sj.Text &amp; vbCrLf &amp; _
      vbCrLf &amp; "Can work around the issue?: " &amp; YNbox.Text &amp; _
      vbCrLf &amp; "Reason For Ticketing: " &amp; Rbox.Text &amp; _
      vbCrLf &amp; "Department: " &amp; Dbox.Text &amp; _
      vbCrLf &amp; "Impact: " &amp; Ibox.Text &amp; _
      vbCrLf &amp; "Urgency: " &amp; Ubox.Text &amp; _
      vbCrLf &amp; "System/Machine Number: " &amp; Mbox.Text &amp; _
      vbCrLf &amp; "Was trying to accomplish: " &amp; Abox.Text &amp; _
      vbCrLf &amp; "Has it occured before?: " &amp; Bbox.Text &amp; _
      vbCrLf &amp; "First Noticed: " &amp; Tbox.Text &amp; _
      vbCrLf &amp; "Others affected by the issue: " &amp; Affbox.Text &amp; _
      vbCrLf &amp; "Additonal Comments: " &amp; Addbox.Text
   Send
  End Sub
  `
  </code></pre>
  
  <p>Sub I found online that is suppose to take an item and append the text onto message body. There seems to be an error every time I try to declare the object's type (Ex. ____ As _____). I was not sure how to modify this to make it fit with my code, but the error is thrown at <code>Dim objItem As Object</code> that says </p>
  
  <blockquote>
    <p>Expected end of statement</p>
  </blockquote>
  
  <pre><code>Sub TestAppendText()
  Dim objItem As Object
  Dim thisMail As Outlook.MailItem
  'On Error Resume Next
  
  Set objItem = Application.ActiveExplorer.Selection(1)
  If Not objItem Is Nothing Then
      If objItem.Class = olMail Then
          Set thisMail = objItem
          Call AppendTextToMessage(thisMail, "Some text added at " &amp; Now())
      End If
  End If
  
  Set objItem = Nothing
  Set thisMail = Nothing
  End Sub
  
  Sub AppendTextToMessage(ByVal objMail As Outlook.MailItem, ByVal strText As String)
      Dim objCDO As MAPI.Session
      Dim objMsg As MAPI.Message
      Dim objField As MAPI.Field
  
      Set objCDO = CreateObject("MAPI.Session")
      objCDO.Logon "", "", False, False
  
      If Not objMail.EntryID = "" Then
          Set objMsg = objCDO.GetMessage(objMail.EntryID, _
                                     objMail.Parent.StoreID)
          objMsg.Text = objMsg.Text &amp; vbCrLf &amp; strText
          objMsg.Update True, True
          Set objField = objMsg.Fields(CdoPR_RTF_COMPRESSED)
          If Not objField Is Nothing Then
              objField.Delete
              objMsg.Update True, True
          End If
          Set objField = Nothing
          Set objField = objMsg.Fields(CdoPR_RTF_SYNC_BODY_COUNT)
          If Not objField Is Nothing Then
              objField.Delete
              objMsg.Update True, True
          End If
       Else
          strMsg = "You must save the item before you add text. " &amp; _
                   "Do you want to save the item now?"
          intAns = MsgBox(strMsg, vbYesNo + vbDefaultButton1, "Append Text to Message")
          If intAns = vbYes Then
              Call AppendTextToMessage(objMail, strText)
          Else
              Exit Sub
          End If
      End If
  
      Set objMsg = Nothing
      objCDO.Logoff
      Set objCDO = Nothing
  End Sub
  </code></pre>
  
  <p>I've also tried using a CDO object, but it gives me an error that says </p>
  
  <blockquote>
    <p>ActiveX component can't create object: 'CDONTS.NewMail'</p>
  </blockquote>
  
  <pre><code>Set Item1 = Item.MessageClass
  Set objCDONTS = CreateObject ("CDONTS.NewMail")
  objCDONTS.Body = Tbox8.Text
  
  Item1.Message = "Hi"
  </code></pre>
  
  <p>I apologize if this seems very simple to others.. I just have 0 experience with VBA/VBScript and Outlook designs. That's why I'm here, to learn!</p>