1

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?

3
  • 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. Commented Apr 2, 2016 at 5:33
  • I think this could help you: Stripping Chinese Characters with VBA Commented Apr 2, 2016 at 7:57
  • It's song lyrics. So I'll most likely just check the first word/character of the title or verse. Commented Apr 3, 2016 at 10:16

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

This 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.
See 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.
Thanks, this works a treat! I actually didn't know about Runs, Sentences, Paragraphs, etc. until I read your edited answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.