通过电子邮件搜索以查找并突出显示文本
Search through an email to find and highlight text
我正在与 Excel 的 VBA 合作,尝试自动发送一些电子邮件。现在,代码根据单击的按钮旁边的单元格创建电子邮件,将电子邮件转发给单元格中列出的人,然后根据更多单元格插入特定的正文消息。单元格中的内容并不重要,但我需要做的是在原始转发邮件中搜索特定文本,如果找到,则需要突出显示该文本。
我的代码如下:
Sub Asset_email()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Outlook.MailItem
Dim i As Integer
Dim olMsg As Outlook.MailItem
Dim r As Range
Dim strLocation As String
Dim o As Outlook.Application
Dim strbody As String
'Dim olAtt As Outlook.Attachments
'Set olAtt = olMsg.Attachments
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("Asset Notifications Macro")
i = 1
For Each olMail In Fldr.Items
If InStr(olMail.body, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value) <> 0 Then
olMail.display
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Team,<br><br>" & _
"Please see the notice below regarding " & _
ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-2).Value & _
".<br><br> Feel free to email the CSG team at myemailhere@email.com with any questions.<br><br>" & _
"Thank you!"
With olMail.Forward
.To = ActiveCell.Offset(ColumnOffset:=-1)
.display
SendKeys ("%")
SendKeys ("7")
'Call Sleep
Application.Wait (Now + TimeValue("0:00:03"))
.HTMLBody = strbody & "<br>" & .HTMLBody
End With
End If
Next
End Sub
代码 100% 有效。我只是不知道搜索和突出显示结果的正确语法。
在上面的示例中,假设我想查找并突出显示单词 "Thank you"。如何解决这个问题?
您基本上必须使用 Word 对象模型来进行文本搜索和突出显示。这篇文章中有一个完美的例子:https://msdn.microsoft.com/en-us/library/gg193974.aspx
Outlook 对象模型为工作项正文提供了三种主要方式:
- Body - 表示 Outlook 项目的明文正文的字符串。
- HTMLBody - 表示指定项目的 HTML 正文的字符串。在这种情况下,您需要解析 HTML 标记并添加周围标记以突出显示文本。例如,您可以将
<b>
用于粗体符号。
- Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to work on the message body. The Bold 属性 的字体 class 允许将文本格式化为粗体。
您可以在 Chapter 17: Working with Item Bodies 中阅读有关所有这些方式的更多信息。选择哪种方式(HTML 或 Word 对象模型)来突出显示文本由您决定。
在活动检查器中突出显示?请尝试以下操作。
您可以将行 set objInspector = Application.ActiveInspector
替换为 set objInspector = olMail.GetInspector
wdColorYellow = 65535
set objInspector = Application.ActiveInspector
set objDoc = objInspector.WordEditor
set objFind = objDoc.Content.Find
objFind.HitHighlight "text_to_highlight", wdColorYellow, , false, true
想通了:
.HTMLBody = Replace(.HTMLBody, "Thank you", "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & "Thank you" & "</FONT>")
我正在与 Excel 的 VBA 合作,尝试自动发送一些电子邮件。现在,代码根据单击的按钮旁边的单元格创建电子邮件,将电子邮件转发给单元格中列出的人,然后根据更多单元格插入特定的正文消息。单元格中的内容并不重要,但我需要做的是在原始转发邮件中搜索特定文本,如果找到,则需要突出显示该文本。
我的代码如下:
Sub Asset_email()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Outlook.MailItem
Dim i As Integer
Dim olMsg As Outlook.MailItem
Dim r As Range
Dim strLocation As String
Dim o As Outlook.Application
Dim strbody As String
'Dim olAtt As Outlook.Attachments
'Set olAtt = olMsg.Attachments
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("Asset Notifications Macro")
i = 1
For Each olMail In Fldr.Items
If InStr(olMail.body, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value) <> 0 Then
olMail.display
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Team,<br><br>" & _
"Please see the notice below regarding " & _
ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-2).Value & _
".<br><br> Feel free to email the CSG team at myemailhere@email.com with any questions.<br><br>" & _
"Thank you!"
With olMail.Forward
.To = ActiveCell.Offset(ColumnOffset:=-1)
.display
SendKeys ("%")
SendKeys ("7")
'Call Sleep
Application.Wait (Now + TimeValue("0:00:03"))
.HTMLBody = strbody & "<br>" & .HTMLBody
End With
End If
Next
End Sub
代码 100% 有效。我只是不知道搜索和突出显示结果的正确语法。
在上面的示例中,假设我想查找并突出显示单词 "Thank you"。如何解决这个问题?
您基本上必须使用 Word 对象模型来进行文本搜索和突出显示。这篇文章中有一个完美的例子:https://msdn.microsoft.com/en-us/library/gg193974.aspx
Outlook 对象模型为工作项正文提供了三种主要方式:
- Body - 表示 Outlook 项目的明文正文的字符串。
- HTMLBody - 表示指定项目的 HTML 正文的字符串。在这种情况下,您需要解析 HTML 标记并添加周围标记以突出显示文本。例如,您可以将
<b>
用于粗体符号。 - Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to work on the message body. The Bold 属性 的字体 class 允许将文本格式化为粗体。
您可以在 Chapter 17: Working with Item Bodies 中阅读有关所有这些方式的更多信息。选择哪种方式(HTML 或 Word 对象模型)来突出显示文本由您决定。
在活动检查器中突出显示?请尝试以下操作。
您可以将行 set objInspector = Application.ActiveInspector
替换为 set objInspector = olMail.GetInspector
wdColorYellow = 65535
set objInspector = Application.ActiveInspector
set objDoc = objInspector.WordEditor
set objFind = objDoc.Content.Find
objFind.HitHighlight "text_to_highlight", wdColorYellow, , false, true
想通了:
.HTMLBody = Replace(.HTMLBody, "Thank you", "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & "Thank you" & "</FONT>")