检测Word文档的语言 [英] Detect language of Word document
问题描述
使用Range.DetectLanguage
,如何检测Word
文档每个段落的语言并确定Word文档最常用的语言?
Using Range.DetectLanguage
, how can I detect the language of each of the paragraphs of a Word
document and determine the most used language of the Word document?
我希望对其进行处理的文档集可以是法语或英语,但是所有文档的标头都将包含英语和法语,因此我不能使用Document.DetectLanguage
,因为这会在所有文档上返回WdUndefined
.我需要检查所有段落并确定文档中最流行的语言.
The set of documents I wish to run this over can be either French or English, but all will have both English and French in the header, so I cannot use Document.DetectLanguage
because this returns WdUndefined
on all documents. I need to check all paragraphs and determine what is the most popular language in the document.
在VBA
中执行此操作的最佳方法是什么?
What is the best way to do this in VBA
?
推荐答案
Dim doc As Document, para As Paragraph
Dim lang As WdLanguageId
Dim dict As New Dictionary
Set doc = ActiveDocument
If Not doc.LanguagedDetected Then doc.DetectLanguage
' count languages in paragraphs
For Each para In doc.Paragaphs
lang = para.Range.LanguageId
If Not dict.Exists(lang) Then
dict.add lang, 1
Else
dict(lang) = dict(lang) + 1
End if
Next
' determine most popular language
Dim maxCount As Integer, maxKey As wdLanguageId
For Each key In dict.Keys()
If dict(key) > maxCount Then
maxCount = dict(key)
maxKey = key
End if
Next
Debug.Print "Most popular language is: " & maxKey
这篇关于检测Word文档的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!