テキストがキリル文字であるかどうかを確認するにはどうすればよいですか?
-
03-07-2019 - |
質問
迷惑メールフォルダーが、キリル文字のように見えるメッセージでいっぱいになりました。メッセージ本文またはメッセージの件名がキリル文字である場合、完全に削除したい。
画面にキリル文字が表示されますが、Outlook内のVBAでメッセージを反復処理すると、" Subject"メッセージのプロパティは疑問符を返します。
メッセージの件名がキリル文字であるかどうかを確認するにはどうすればよいですか?
(注:「InternetCodepage」プロパティを調べました。通常は西ヨーロッパ言語です。)
解決
VB / VBAの String
データ型はUnicode文字を処理できますが、IDE自体にはUnicode文字の表示に問題があります(そのため疑問符)。
私はあなたを助けるかもしれない IsCyrillic
関数を書きました。この関数は、単一の String
引数を取り、文字列に少なくとも1つのキリル文字が含まれている場合、 True
を返します。このコードをOutlook 2007でテストしましたが、うまく機能しているようです。それをテストするために、件名にキリル文字を含む電子メールをいくつか送信し、テストコードが受信ボックス内のすべての中からそれらの電子メールを正しく選択できることを確認しました。
つまり、実際には2つのコードスニペットがあります:
-
IsCyrillic
関数を含むコード。これはコピー&ペーストできます 新しいVBAモジュールに追加するか、 既に持っているコード。 - コードが実際に機能することをテストするために(Outlook 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
他のヒント
&quot; Subject&quot;メッセージのプロパティは疑問符の束を返します。
古典的な文字列エンコードの問題。そのプロパティはASCIIを返しているように聞こえますが、UTF-8またはUnicodeが必要です。
あなたにはすでに簡単な解決策があるようです-(たとえば)疑問符が5つ付いている件名を探してください