none
Removing emoji from string RRS feed

  • Question

  • Hi,

    Id like to ask how can i remove all emoji from string in vba? It would be nice to keep Latin Extended-A chars, but its not really that important

    Thanks in advance

    Monday, July 1, 2019 5:52 PM

Answers

  • Can you give us an example or two of such string to see the type of data you are dealing with.

    Where is the string originating from, what format is it in?

    You could perhaps try something along the lines of the following to exclude chars outside of the standard ascii char set.

    Public Function CleanString(sInput As String) As String
        Dim sChar                 As String
        Dim lStrLen               As Long
        Dim i                     As Long
        Dim iAscW                 As Integer
        
        lStrLen = Len(sInput)
        For i = lStrLen To 1 Step -1
            sChar = Mid(sInput, i, lStrLen)
            iAscW = AscW(sChar)
            If iAscW < lStrLen Or iAscW > 256 Then
                sInput = Replace(sInput, sChar, "")
            End If
        Next i
    
        CleanString = sInput
    End Function


    Daniel Pineault, 2010-2018 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net



    Tuesday, July 2, 2019 1:23 AM

All replies

  • Can you give us an example or two of such string to see the type of data you are dealing with.

    Where is the string originating from, what format is it in?

    You could perhaps try something along the lines of the following to exclude chars outside of the standard ascii char set.

    Public Function CleanString(sInput As String) As String
        Dim sChar                 As String
        Dim lStrLen               As Long
        Dim i                     As Long
        Dim iAscW                 As Integer
        
        lStrLen = Len(sInput)
        For i = lStrLen To 1 Step -1
            sChar = Mid(sInput, i, lStrLen)
            iAscW = AscW(sChar)
            If iAscW < lStrLen Or iAscW > 256 Then
                sInput = Replace(sInput, sChar, "")
            End If
        Next i
    
        CleanString = sInput
    End Function


    Daniel Pineault, 2010-2018 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net



    Tuesday, July 2, 2019 1:23 AM

  •         If iAscW < lStrLen Or iAscW > 256 Then
    

    Hi Daniel,

    What is the functionality of:    iAscW < lStrLen ? I do not understand this condition.

    I think it would be a little bit more accurate if you use:   iAscW > 255 .

    Imb.

    Tuesday, July 2, 2019 8:49 PM
  • Thank you, its exactly what i was looking for. Intended usage is to strip all email communication of emoticons as i cannot push them tru driver to serve.

    There are some changes needed tho:

    iAscW < lStrLen // must be iAscW < 0

    sChar = Mid(sInput, i, 1) //with 1 so only one char will be replaced

    Replace(sInput, sChar, "") //replacement string cannot be empty or it will fail if the first (last) char is one to change - this is btw very strange behavior:

    e😃   //stripped

    e😃😃   //error

    e😃😃e    //stripped

    e😃😃😃e   //error

    e😃😃😃ee     //stripped

    and so on, yet only with empty string as replace

    Anyway, thanks for help

    Wednesday, July 3, 2019 2:48 PM