我的“垃圾邮件”文件夹里塞满了似乎是西里尔字母的邮件。如果邮件正文或邮件主题使用西里尔文,我希望将其永久删除。
我在屏幕上看到的是西里尔字母,但当我在Outlook的VBA中迭代邮件时,邮件的"Subject“属性会返回问号。
如何确定邮件主题是否为西里尔字符?
(注意:我已经检查了"InternetCodepage“属性-它通常是西欧的。)
发布于 2008-10-16 03:17:53
VB/VBA中的String数据类型可以处理Unicode字符,但是集成开发环境本身很难显示这些字符(因此出现了问号)。
我编写了一个IsCyrillic函数,它可能会对您有所帮助。该函数接受单个String参数,如果字符串至少包含一个西里尔字母,则返回True。我用Outlook2007测试了这段代码,它似乎工作得很好。为了测试它,我给自己发了几封主题为西里尔文的电子邮件,并验证了我的测试代码可以正确地从我的收件箱中识别出这些电子邮件。
所以,我实际上有两个代码片段:
IsCyrillic函数的代码。这可以复制粘贴到新的VBA模块中,也可以添加到您已有的代码中。Test例程,以测试代码是否实际工作。它演示了如何使用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发布于 2008-10-15 22:20:54
消息的"Subject“属性返回一堆问号。
一个典型的字符串编码问题。听起来该属性返回ASCII,但您需要UTF-8或Unicode。
发布于 2008-10-15 23:10:09
在我看来,您已经有了一个简单的解决方案--只需查找任何带有(比方说)5个问号的主题行
https://stackoverflow.com/questions/206719
复制相似问题