我正在使用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%正常运行。我只是不知道正确的搜索语法并突出显示结果。
在上面的例子中,假设我想找到并突出显示单词&#34;谢谢你&#34;。怎么会这样呢?
答案 0 :(得分:1)
您基本上必须使用Word对象模型进行文本搜索和突出显示。在本文中有一个完美的示例:https://msdn.microsoft.com/en-us/library/gg193974.aspx
答案 1 :(得分:1)
在活动的检查器中突出显示?请尝试以下方法。
您可以使用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
答案 2 :(得分:0)
Outlook对象模型为工作项主体提供了三种主要方式:
<b>
用于粗体符号。 您可以在Chapter 17: Working with Item Bodies中详细了解所有这些方式。我们可以选择哪种方式(HTML或Word对象模型)来突出显示文本。
答案 3 :(得分:0)
想出来:
.HTMLBody = Replace(.HTMLBody, "Thank you", "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & "Thank you" & "</FONT>")