【问题标题】:Paste Text in specified position将文本粘贴到指定位置
【发布时间】:2020-11-26 21:13:11
【问题描述】:

我根据需要修改了搜索代码。

我需要将粘贴放置在 xOutMsg 变量中的“条件”文本下。它正在粘贴到电子邮件的第一行。

如何在“条件”一词之后的下一行粘贴?

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

【问题讨论】:

标签: vba outlook outlook-2010


【解决方案1】:

您似乎需要解析消息并在其中插入您自己的文本或标记。 MailItem.HTMLBody 属性设置表示指定项的 HTML 正文的字符串。使用InStr 函数,该函数返回一个long,指定一个字符串在另一个字符串中第一次出现的位置。获得关键字位置后,您可以在关键字之后插入子字符串。

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

在 MSDN 中阅读有关 InStr function 的更多信息。

Outlook 对象模型支持自定义邮件正文的三种主要方式:

  1. Body 属性返回或设置表示 Outlook 项目的明文正文的字符串。
  2. MailItem 类的HTMLBody 属性返回或设置表示指定项目的HTML 正文的字符串。设置 HTMLBody 属性将始终立即更新 Body 属性。例如:
     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. Word 对象模型可用于处理消息体。请参阅Chapter 17: Working with Item Bodies 了解更多信息。

没有必要将HTMLBodyWordEditor 方法混合在一起。您可以选择一种方式并使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 2018-11-14
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2021-06-19
    • 1970-01-01
    相关资源
    最近更新 更多