我正在使用 Excel 尝试自动发送一些电子邮件。目前,代码根据单击的按钮旁边的单元格创建一封电子邮件,将电子邮件转发到单元格中列出的内容,然后根据其他单元格插入特定的正文消息。单元格中的内容并不重要,但我需要做的是在原始转发消息中搜索特定文本,如果找到,则需要突出显示该文本。
我的代码如下:
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 [email protected] 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% 有效。我只是不知道搜索和突出显示结果的正确语法。
在上面的例子中,假设我想查找并突出显示单词“谢谢”。该怎么做呢?
答案1
尝试这个:
.HTMLBody = Replace(.HTMLBody, "Thank you", "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & "Thank you" & "</FONT>")