0

I have code from searches modified for my need.

I need to position the paste under the "Conditions" text in the xOutMsg variable. It is pasting into the first line in the email.

How do I paste on the next line after the word "Conditions"?

Sub PrepareConditionsEmail()
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Dim xOutApp As Object
Dim xOutMail As Object
Dim xOutMsg As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xOutMsg = "<br /><br /><b><u>Conditions:</b></u><br /><br />" & "<b><u> </b></u><br />"
With xOutMail
    '.To = "Email Address"
    .CC = ""
    .BCC = ""
    .Subject = "Conditions"
    .HTMLBody = xOutMsg
    .Display
End With
Set xOutMail = Nothing
Set xOutApp = Nothing
On Error Resume Next
Set objDoc = Application.ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.PasteSpecial Link:=False, _
DataType:=wdPasteText, _
Placement:=wdInLine, _
DisplayAsIcon:=False
End Sub
Community
  • 1
  • 1
MEC
  • 233
  • 3
  • 12
  • Possible duplicate of [Paste clipboard in outlook email in normal order](https://stackoverflow.com/questions/44693988/paste-clipboard-in-outlook-email-in-normal-order) – niton Aug 06 '20 at 18:33

1 Answers1

0

It seems you need to parse the message and insert your own text or markup there. The MailItem.HTMLBody property sets a string representing the HTML body of the specified item. Use the InStr function which returns a long specifying the position of the first occurrence of one string within another. After you get the keyword position you may insert your substring right after the keyword.

 InStr(mail.HTMLBody, "keyword", 1)

Read more about the InStr function in MSDN.

The Outlook object model supports three main ways of customizing the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.

There is no need to mix HTMLBody and WordEditor approaches together. You can choose a single way and use it.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45