Home > Mobile >  Set specific font size for Hebrew characters in Word in VBA
Set specific font size for Hebrew characters in Word in VBA

Time:11-12

I am in need to modify this code which is made for Word 2007 to Word XP. I need to set size for hebrew text in document. It is mix of Central Europe words combined with hebrew. What I need is hebrew text in bigger size. This contains only basic hebrew characters, no vowels. The non hebrews characters can be English or Czech (http://www.biega.com/special-char.html), the hebrew Chars are from 1488 to 1514.

But I could not run the code send here Flag special characters in document using VBA in Word in the answer as I got error (Range?) object not selected.

Sub test()
    'Options.DefaultHighlightColorIndex = wdNoHighlight
    'Range.HighlightColorIndex = wdNoHighlight ' used for testing to clear Highlight

    Dim r As Range, t As Double: t = Timer
    Application.ScreenUpdating = False

    For Each r In Range.Characters ' For Each r In Range.Words is somehow about 2 times slower than .Characters
        checkRange r
    Next

    Application.ScreenUpdating = True
    Debug.Print Timer - t; Range.Words.Count; Range.Characters.Count; Range.End ' " 3.15625  8801  20601  20601 "
End Sub

Sub checkRange(r As Range)
    Dim b() As Byte, i As Long, a As Long
    b = r.Text ' converts the string to byte array (2 or 4 bytes per character)
    'Debug.Print "'" & r & "'"; r.LanguageID; r.LanguageIDFarEast; r.LanguageIDOther

    For i = 1 To UBound(b) Step 2            ' 2 bytes per Unicode codepoint
        If b(i) > 0 Then                     ' if AscW > 255
            a = b(i): a = a * 256   b(i - 1) ' AscW
            Select Case a
                Case &H1F00 To &H1FFF: r.HighlightColorIndex = wdBlue: Exit Sub ' Greek Extended
                Case &H3040 To &H30FF: r.HighlightColorIndex = wdPink: Exit Sub ' Hiragana and Katakana
                Case &H4E00 To 40959: r.HighlightColorIndex = wdGreen: Exit Sub ' CJK Unified Ideographs

                Case 55296 To 56319: ' ignore leading High Surrogates ?
                Case 56320 To 57343: ' ignore trailing Low Surrogates ?

                Case Else: r.HighlightColorIndex = wdRed: Debug.Print Hex(a), r.End - r.Start ' other
            End Select
        End If
    Next
End Sub

So I only need to select the size and make this working for Word XP. Any help?

Notice: Whole document and the hebrew chars are written from left to right (when I typed hebrew in online keyboard, they were RTL, but after coping to Word they were LTR. But this was not problem, because I have copied/pasted them to display the hebrew word correctly. So they are in LTR in fact.

CodePudding user response:

The error is because you haven't specified which object's range you want to process.

For Each r In Range.Characters

Needs to be qualified either as

For Each r In Selection.Range.Characters

or as

For Each r In ActiveDocument.Range.Characters

You could have answered this for yourself simply by looking up the online help for Range

NOTE: Processing a document character by character is going to be a very long process. If the characters you want to process can be identified by language id you can speed up the process considerably.

  • Related