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?
-
Can you provide more detail about the length and nature of the text? You could just check for the presence of vowels, but it depends on whether your English text is constituted of real English words, or just English letters. For example, if your Powerpoint textbox contained English and Chinese number plates or state codes, searching for Vowels might not be an adequate check. Likewise, if your Chinese text sometimes had English characters, then that would be a problem too. – ThunderFrame Apr 02 '16 at 05:33
-
I think this could help you: [Stripping Chinese Characters with VBA](http://stackoverflow.com/questions/10710518/strip-chinese-characters-from-a-string-vba) – Jamie Garroch - MVP Apr 02 '16 at 07:57
-
It's song lyrics. So I'll most likely just check the first word/character of the title or verse. – Koganei Apr 03 '16 at 10:16
2 Answers
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

- 41,537
- 7
- 86
- 101
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

- 14,442
- 1
- 29
- 34
-
1This only works if the textbox only contains one language. Sometimes my textboxes will contain both Chinese and English. I think Florent's solution above works best for my situation. – Koganei Apr 03 '16 at 10:38
-
1See the edited version of my original answer. I suspect it'll be more reliable and more widely useful than testing the first character of each line of text. – Steve Rindsberg Apr 03 '16 at 15:38
-
Thanks, this works a treat! I actually didn't know about Runs, Sentences, Paragraphs, etc. until I read your edited answer! – Koganei Apr 05 '16 at 01:33