删除电子邮件开头句子中的中文字符

问题描述 投票:0回答:2

我的公司添加了有关来自我公司外部的电子邮件的警告。

此电子邮件来自 ***** 群组外部。除非您认识发件人并且知道内容是安全的,否则请勿单击链接或打开附件。
这是外部寄来的邮件,点击链接或打开附件前请停、看、听。

Security Warnings

我找到了一个可以删除警告句子的宏,但我无法让它删除中文句子。

我尝试将中文句子添加到

InStr
中,但它显示为???.
我尝试将“非 Unicode 程序的语言”更改为中文(简体,中国),但随后宏停止工作。

Option Explicit

Sub myRuleMacro(item As Outlook.MailItem)
Sub RemoveExpressionFOLDER()

Dim outFldr As Folder
Dim outItems As Items
Dim outMailItem As MailItem

Dim i As Long
Dim cleanCount As Long

Set outFldr = ActiveExplorer.CurrentFolder

Set outItems = outFldr.Items

For i = 1 To outItems.Count

    If outItems(i).Class = olMail Then

        Set outMailItem = outItems(i)

        With outMailItem

            'Debug.Print .Subject

            If InStr(.Body, "This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe.") Then

                If .BodyFormat = olFormatHTML Then
                    .HTMLBody = Replace(.HTMLBody, "This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe.", "")
                Else
                    .Body = Replace(.Body, "This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe.", "")
                End If

                .Save

                cleanCount = cleanCount + 1

            End If

        End With

    End If

Next i

MsgBox (cleanCount & " mailitems cleaned.")

End Sub

有没有另一种方法可以将中文句子添加到我的

InStr
中,以便它可以删除这两个句子。或者,如果这是不可能的,也许另一个宏可以实现我的目标。

vba outlook
2个回答
0
投票

这是一种暴力硬编码方法……

中文字符存储为Unicode。您可以使用

ChrW
函数将整数转换为 Unicode,并使用
AscW

将 Unicode 字符转换为其整数值

因此,运行一个循环将每个字符一一转换为整数,并将它们聚合成一个相反的函数,我得到了:

Function ChineseWarning() AS String
    ChineseWarning = ChrW(-28647) & ChrW(26159) & ChrW(22806) & ChrW(-28440) & _
        ChrW(23492) & ChrW(20358) & ChrW(30340) & ChrW(-28427) & _
        ChrW(20214) & ChrW(-244) & ChrW(-24866) & ChrW(25802) & _
        ChrW(-28637) & ChrW(32080) & ChrW(25110) & ChrW(-27253) & _
        ChrW(21855) & ChrW(-27068) & ChrW(20214) & ChrW(21069) & _
        ChrW(-30005) & ChrW(20572) & ChrW(12289) & ChrW(30475) & _
        ChrW(12289) & ChrW(-32643) & ChrW(12290)
End Function

然后您可以在您的

Replace
中使用它:

.HTMLBody = Replace(.HTMLBody, ChineseWarning(), "")

0
投票
  • 创建 UDF 以删除不需要的句子。
  • RegExp可以处理所有汉字和部分标点符号

正则表达式工具

中日韩统一表意文字(Unicode 块)

Function RemoveStr(ByVal inputText As String) As String
    Dim regExp As Object, sWord As String
    sWord = "This email originated from outside of ***** group. " & _
        "Do not click links or open attachments unless you  " & _
        "recognize the sender and know the content is safe."
    inputText = Replace(inputText, "")
    Set regExp = CreateObject("VBScript.RegExp")
    regExp.Pattern = "[\u4E00-\u9FA5\uFF0C\u3001\u3002]+"
    RemoveStr = regExp.Replace(inputText, "")
End Function
  • 更改两行来调用UDF
                If .BodyFormat = olFormatHTML Then
                    .HTMLBody = RemoveStr(.HTMLBody)
                Else
                    .Body = RemoveStr(.Body)
                End If
© www.soinside.com 2019 - 2024. All rights reserved.