Detect text language in VBA

拈花ヽ惹草 提交于 2019-12-11 10:18:11

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/36369353/detect-text-language-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!