Koganei
Koganei

Reputation: 11

Detect text language in VBA

I have a textbox in PowerPoint which I store into an array with Split. Is there any way to detect what language the text is in VBA? There will actually only be English or Chinese text, so I guess an alternative solution would be to detect if the text is not English, or is/isn't Unicode?

Upvotes: 1

Views: 2847

Answers (2)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

The shape's .TextFrame.TextRange.LanguageID will tell you what language the text is set to. US English is 1033, for example. There's a list of language IDs here (use the Decimal LCID, right-hand column in this case):

https://msdn.microsoft.com/en-us/goglobal/bb964664.aspx?f=255&MSPPError=-2147217396

It's worth looking at the hex values as well. The rightmost two digits give you the main language code (Chinese is 04, for example) and the leftmost two digits identify the specific locale (PRC, Singapore, Taiwan, etc).

If you're likely to have mixed language text in a single text box, look at the LanguageID property of each .Run of text. For example, with a shape selected, try this:

Dim oRng As TextRange
Dim x As Long

With ActiveWindow.Selection.ShapeRange(1).TextFrame.TextRange
    For x = 1 To .Runs.Count
        Debug.Print .Runs(x).LanguageID
    Next
End With

Upvotes: 1

Florent B.
Florent B.

Reputation: 42538

It should be possible by checking that one of the characters is Chinese:

Function IsChiness(text As String) As Boolean
  Dim c&, i&
  For i = 1 To Len(text)
    c = AscW(Mid$(text, i, 1))
    If c >= &H4E00& And c <= &H9FFF& Then
      IsChiness = True
      Exit Function
    End If
  Next
End Function

Upvotes: 3

Related Questions