Come posso determinare se il testo è in caratteri cirillici?
-
03-07-2019 - |
Domanda
La mia cartella di posta indesiderata si è riempita di messaggi composti in quello che sembra essere l'alfabeto cirillico. Se un corpo del messaggio o l'oggetto del messaggio è in cirillico, voglio eliminarlo definitivamente.
Sul mio schermo vedo caratteri cirillici, ma quando eseguo l'iterazione dei messaggi in VBA in Outlook, l'oggetto " Oggetto " proprietà del messaggio restituisce punti interrogativi.
Come posso determinare se l'oggetto del messaggio è in caratteri cirillici?
(Nota: ho esaminato la proprietà " InternetCodepage " di solito è dell'Europa occidentale.)
Soluzione
Il tipo di dati String
in VB / VBA può gestire i caratteri Unicode, ma l'IDE stesso ha difficoltà a visualizzarli (da qui i punti interrogativi).
Ho scritto una funzione IsCyrillic
che potrebbe aiutarti. La funzione accetta un singolo argomento String
e restituisce True
se la stringa contiene almeno un carattere cirillico. Ho testato questo codice con Outlook 2007 e sembra funzionare bene. Per provarlo, mi sono inviato alcune e-mail con il testo in cirillico nella riga dell'oggetto e ho verificato che il mio codice di prova potesse correttamente rilevare quelle e-mail tra tutte le altre nella mia Posta in arrivo.
Quindi, in realtà ho due frammenti di codice:
- Il codice che contiene la funzione
IsCyrillic
. Questo può essere incollato in un nuovo modulo VBA o aggiunto a il codice che hai già. - La routine
Test
che ho scritto (in Outlook VBA) per verificare che il codice funzioni effettivamente. Viene illustrato come utilizzare la funzioneIsCyrillic
.
Il codice
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
Esempio di utilizzo
' 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
Altri suggerimenti
il " Oggetto " proprietà del messaggio restituisce un gruppo di punti interrogativi.
Un classico problema di codifica delle stringhe. Sembra che quella proprietà stia restituendo ASCII ma vuoi UTF-8 o Unicode.
Mi sembra che tu abbia già una soluzione semplice: cerca qualsiasi argomento con (diciamo) 5 punti interrogativi