我的垃圾邮件已填充信息组成,这似乎是西里尔字母。如果一个信息体或一个信息问题是在西里尔文,我要永久删除它。

在我的画面我看到西里尔文字,但当我迭代的消息在VBA内的展望,"主题"酒店的信息返回问题的痕迹。

我怎么能确定如果该问题的消息是在西里尔字?

(注:我已经审查了"InternetCodepage"酒店-这是从西欧洲。)

有帮助吗?

解决方案

String 数据类型中VB/VBA可以处理Unicode字,但IDE本身具有的麻烦,显示它们(因此,这个问题标记)。

我写了一个 IsCyrillic 功能可以帮助你。功能需要一个单一的 String 参数和返回 True 如果串至少包含一个西里尔字符。我测试了这个代码Outlook2007年,它似乎正常工作。来测试它,我给自己一些电子邮件用西里尔字母文本的主题并确认我的测试码可以正确地选择了那些电子邮件之间的其他一切都在我的收件箱。

因此,我其实有两段代码:

  • 代码包含 IsCyrillic 功能。这可以复制贴 进入一个新的VBA模块或加入 代码你已经有了。
  • Test 惯例我写的(在Outlook VBA)测试,代码的实际工作。它展示了如何使用 IsCyrillic 功能。

代码

Option Explicit

Public Const errInvalidArgument = 5

' Returns True if sText contains at least one Cyrillic character'
' NOTE: Assumes UTF-16 encoding'

Public Function IsCyrillic(ByVal sText As String) As Boolean

    Dim i As Long

    ' Loop through each char. If we hit a Cryrillic char, return True.'

    For i = 1 To Len(sText)

        If IsCharCyrillic(Mid(sText, i, 1)) Then
            IsCyrillic = True
            Exit Function
        End If

    Next

End Function

' Returns True if the given character is part of the Cyrillic alphabet'
' NOTE: Assumes UTF-16 encoding'

Private Function IsCharCyrillic(ByVal sChar As String) As Boolean

    ' According to the first few Google pages I found, '
    ' Cyrillic is stored at U+400-U+52f                '

    Const CYRILLIC_START As Integer = &H400
    Const CYRILLIC_END  As Integer = &H52F

    ' A (valid) single Unicode char will be two bytes long'

    If LenB(sChar) <> 2 Then
        Err.Raise errInvalidArgument, _
            "IsCharCyrillic", _
            "sChar must be a single Unicode character"
    End If

    ' Get Unicode value of character'

    Dim nCharCode As Integer
    nCharCode = AscW(sChar)

    ' Is char code in the range of the Cyrillic characters?'

    If (nCharCode >= CYRILLIC_START And nCharCode <= CYRILLIC_END) Then
        IsCharCyrillic = True
    End If

End Function


例的使用

' On my box, this code iterates through my Inbox. On your machine,'
' you may have to switch to your Inbox in Outlook before running this code.'
' I placed this code in `ThisOutlookSession` in the VBA editor. I called'
' it in the Immediate window by typing `ThisOutlookSession.TestIsCyrillic`'

Public Sub TestIsCyrillic()

    Dim oItem As Object
    Dim oMailItem As MailItem

    For Each oItem In ThisOutlookSession.ActiveExplorer.CurrentFolder.Items

        If TypeOf oItem Is MailItem Then

            Set oMailItem = oItem

            If IsCyrillic(oMailItem.Subject) Then

                ' I just printed out the offending subject line '
                ' (it will display as ? marks, but I just       '
                ' wanted to see it output something)            '
                ' In your case, you could change this line to:  '
                '                                               '
                '     oMailItem.Delete                          '
                '                                               '
                ' to actually delete the message                '

                Debug.Print oMailItem.Subject

            End If

        End If

    Next

End Sub

其他提示

  

“主题”消息的属性返回一堆问号。

经典的字符串编码问题。听起来这个属性正在返回ASCII,但你需要UTF-8或Unicode。

在我看来,你已经有了一个简单的解决方案 - 只需查找任何带有(例如)5个问号的主题行

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top