none
Vba to Hide Chinese characters RRS feed

  • Question

  • Hi all! I have a question! I want to write a macro to hide only Chinese characters in word document but I have no idea. Could you please help me? Thanks a lot!!
    Monday, February 25, 2019 12:30 AM

All replies

  • Hi Rainbow Walker, I'd like confirm: What do you mean by "hide"? Does it mean "delete" or "remove", or make Chinese Character white? Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Monday, February 25, 2019 6:09 AM
  • Hi Rainbow Walker,

    I assume you want to "Delete" Chinese characters (including Japanese, Korean, and not ASCII characters).
    I've made a sample.


    Private Sub btn_Hide_CJK_Click()
        Application.ScreenUpdating = False
        ' ---
        Dim oneWord As String
        Dim oneChar As String
        ' --- separete character as a word in this document
        Dim i As Integer
        For i = ActiveDocument.Words.Count To 1 Step -1
            oneWord = ActiveDocument.Words(i).Text
            oneChar = Left(oneWord, 1)
            If (Asc(oneChar) < 0) Or (Asc(oneChar) > 255) Then
                ' --- not ASCII: CJK character
                ' MsgBox "-- Chinese Character --" & vbLf & "oneWord = " & oneWord
                Call prc_HideChinese(oneWord)
            End If
        Next i
        ' ---
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub prc_HideChinese(ByVal oneWord As String)
        ActiveDocument.Select
        ' --- replace oneWord with null: delete oneWord
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = oneWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        ' --- move caret to the top of document
        Selection.Collapse Direction:=wdCollapseStart
    End Sub
    Regards,

    P.S.
      Sorry, I could not delete Hangul characters. Now, I'm learning how-to. 


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    • Edited by Ashidacchi Monday, February 25, 2019 6:43 AM
    Monday, February 25, 2019 6:21 AM
  • Thanks for your reply. I mean this one

    https://www.online-tech-tips.com/ms-office-tips/word-hidden-text/ 

    Monday, February 25, 2019 3:40 PM
  • Wow! Thanks a lot. But I want to hide them but not delete them. 

    https://www.online-tech-tips.com/ms-office-tips/word-hidden-text/

    Monday, February 25, 2019 3:41 PM
  • Hi Raibow Walker,

    I've made a sample (it does not work perfect).


    "Selection.Font.Hidden = False" does not work as expected.
    So, I made its color white. Please examine this code.

     ===[Hide CJK] button=========================
    Private Sub btn_Make_CJK_Hidden_Click()
        Application.ScreenUpdating = False
        ActiveDocument.Select
        Selection.Font.Hidden = False
        ' ---
        Dim oneWord As String
        Dim oneChar As String
        ' --- separete character as a word in this document
        Dim i As Integer
        For i = ActiveDocument.Words.Count To 1 Step -1
            oneWord = ActiveDocument.Words(i).Text
            oneChar = Left(oneWord, 1)
            If (Asc(oneChar) < 0) Or (Asc(oneChar) > 255) Then
                ' --- not ASCII: CJK character
                ' -- MsgBox "-- Chinese Character --" & vbLf & "oneWord = " & oneWord
                Call prc_Hidden(oneWord)
            End If
        Next i
        ' ---
        Application.ScreenUpdating = True
    End Sub
    ' --- Make CJK characters Hidden
    Private Sub prc_Hidden(ByVal oneWord As String)
        ActiveDocument.Select
        ' ---
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindContinue
        End With
        ' ---
        Selection.Find.Execute findtext:=oneWord
        Selection.Font.Hidden = True    ' --<< This does NOT work
        Selection.Font.ColorIndex = wdWhite
        ' --- move caret to the top of document
        Selection.Collapse Direction:=wdCollapseStart
    End Sub
    
    ' ===[View Hidden Text] button=========================
    Private Sub btn_View_Hidden_Text_Click()
        Application.ScreenUpdating = False
        ActiveDocument.Select
        Selection.Font.Hidden = False
        ' ---
        Dim oneWord As String
        Dim oneChar As String
        ' --- separete character as a word in this document
        Dim i As Integer
        For i = ActiveDocument.Words.Count To 1 Step -1
            oneWord = ActiveDocument.Words(i).Text
            oneChar = Left(oneWord, 1)
            If (Asc(oneChar) < 0) Or (Asc(oneChar) > 255) Then
                ' --- not ASCII: CJK character
                ' -- MsgBox "-- Chinese Character --" & vbLf & "oneWord = " & oneWord
                Call prc_View(oneWord)
            End If
        Next i
        ' ---
        Application.ScreenUpdating = True
    End Sub
    ' --- make CJK characters Visible
    Private Sub prc_View(ByVal oneWord As String)
        ActiveDocument.Select
        ' ---
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindContinue
        End With
        ' ---
        Selection.Find.Execute findtext:=oneWord
        Selection.Font.Hidden = False    ' --<< This does NOT work
        Selection.Font.ColorIndex = wdRed
        ' --- move caret to the top of document
        Selection.Collapse Direction:=wdCollapseStart
    End Sub
    

    Regards,

    P.S.
      Next time when you ask a question and if you have some reference article, I hope you will provide it in advance.


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Tuesday, February 26, 2019 12:31 AM