none
How to find the detected language from Word File? RRS feed

  • Question

  • In application I can see there is an option for detectlanguage in word interop.

    document.DetectLanguage();            
    var res = document.LanguageDetected;

    while debugging the code from the watch window i can see LanguageDetected= true 

    but I am unable to find that language name.  I loop through the each word and find the language used for that word

    document.Words[i].LanguageID for every time I got 1033 as id. Is it possible to get the detracted language  or not?



    Friday, October 21, 2016 6:33 AM

All replies

  • Hi,

    According to Range.DetectLanguage Method (Word)

    The results of the DetectLanguage method are stored in the LanguageID property on a character-by-character basis.

    Besides, based on WdLanguageID Enumeration, 1033 represent wdEnglishUS

    What do you mean "detracted language" ? Do you want to check if the language of one specific word is changed?

    Friday, October 21, 2016 9:33 AM
    Moderator
  • @Celeste Li thanks for your time. No, I Want to find auto detected proofing Language name.
    Friday, October 21, 2016 11:46 AM
  • You could use a macro like:

    Sub ListLanguages()
    Dim SBar As Boolean, Lang As Language, Rng As Range, StrLangs As String
    Application.ScreenUpdating = False
    ' Store current Status Bar status, then switch on
    SBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    For Each Lang In Languages
       Application.StatusBar = "Now testing: " & Lang
       For Each Rng In ActiveDocument.StoryRanges
         With Rng.Find
           .Format = True
           .Forward = True
           .Wrap = wdFindContinue
           .LanguageID = Lang.ID
           .Execute
           If .Found = True Then
             StrLangs = StrLangs & vbCr & Lang.NameLocal
             Exit For
           End If
         End With
       Next
    Next
    Application.ScreenUpdating = True
    Application.StatusBar = ""
    Application.StatusBar = SBar
    MsgBox "The languages used in this document are:" & StrLangs
    End Sub

    I'll leave it to you to do the C# conversion.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, October 21, 2016 10:46 PM
  • Hi thanks for your time. This is what I looking for. but getting issue. below are the detail.

            internal List<string> GetLanguageInfo()
            {
                bool SBar;
                Program.application.ScreenUpdating = false;
                SBar = Program.application.DisplayStatusBar;
                List<string> langList = new List<string>();
                foreach (Word.Language Lang in Program.application.Languages)
                {
                    Program.application.StatusBar = "Now testing: " + Lang;
                    foreach (Word.Range Rng in Program.application.ActiveDocument.StoryRanges)
                    {
                        if (Rng.LanguageID == Lang.ID)
                        {
                            langList.Add(Lang.NameLocal);
                        }
                    }
                }
                Program.application.ScreenUpdating = true;
                Program.application.StatusBar="";
                //Program.application.StatusBar = SBar;
                return langList;
            }

    Here is my c# code. I Uploaded a file to https://1drv.ms/w/s!Ah-Jh2Ok5SuHb3uPLrF-a1U5JP0  to this location. That file contains 2 languages German and Thai. While debugging I got 5 Story ranges. with LanguageID 9999999,wdEnglishUS, wdEnglishUS, wdEnglishUS, wdEnglishUS. I also checked with individual language (i.e. I created a file with only Thai Text) then also I got 5 Story ranges one respective Ids for the languages(i.e. For Thai I got wdThai and for German: wdGerman) and four Ids for English. From my research I can say the above code is working perfectly on a docx file, only if the docx file contain one language.

    I also added 3 language to a file and run the script on, it behaves same as 2 languages(i.e detected 5 ranges with four English and one 9999999)

    Can you please guide me so that I can find the proper language List.


    Monday, October 24, 2016 11:39 AM
  • The 9999999 means only that the story range contains more than one language. That's why the code I posted uses Find/Replace. I explained the logic in your thread on finding fonts (https://social.msdn.microsoft.com/Forums/office/en-US/27d18a3e-8b5b-459b-aa78-fb422fac40f5/how-to-get-all-the-fonts-used-in-a-document-using-interop-c?forum=worddev) where the code you're using will produce exactly the same results where more than one font is used in a paragraph. For whatever reason, you marked that reply as an answer, then unmarked it again. Perhaps now you will understand the issue?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, October 24, 2016 11:18 PM
  • Thank you @Paul, Is it possible to detect individual language? Yesterday I tried but cant find any solution.  and  for  finding fonts I didnt try your code and bymistake I ckick mark as answer link
    Wednesday, October 26, 2016 6:21 AM
  • The code I posted tests all languages. If you know what languages a document might contain, you could use an array of the possible language IDs and look for those. For example, if you know the only languages that could be used in a document are UK English, US English & French, you might use code like:

    Sub ListLanguages()
    Dim SBar As Boolean, i As Long, Rng As Range
    Dim ArrLangs, StrLangs As String, Lang As Language
    ArrLangs = Array(wdEnglishUK, wdEnglishUS, wdFrench)
    Application.ScreenUpdating = False
    ' Store current Status Bar status, then switch on
    SBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    For i = 0 To UBound(ArrLangs)
      Set Lang = Languages(ArrLangs(i))
      Application.StatusBar = "Now testing: " & Lang.NameLocal
      For Each Rng In ActiveDocument.StoryRanges
        With Rng.Find
          .Format = True
          .Forward = True
          .Wrap = wdFindContinue
          .LanguageID = Lang.ID
          .Execute
          If .Found = True Then
            StrLangs = StrLangs & vbCr & Lang.NameLocal
            Exit For
          End If
        End With
      Next
    Next
    Application.ScreenUpdating = True
    Application.StatusBar = ""
    Application.StatusBar = SBar
    MsgBox "The languages used in this document are:" & StrLangs
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, October 28, 2016 6:04 AM
  • I need to get the languages of the contents which are in word,presentation and excel files using c# interop. On going through the earlier replies in this thread, I was able to workaround and get language id's for word and presentation files.
    Code snippet for Word:
    foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in docs.Paragraphs)
                    {
                        object pStart = paragraph.Range.Start;
                        object pEnd = paragraph.Range.End;
                        Microsoft.Office.Interop.Word.Range rangeForpara = docs.Range(ref pStart, ref pEnd);
                        var paralangid = rangeForpara.LanguageID;
                        langList.Add(paralangid.ToString());
                    }
    The above piece of code gives me the language for each paragraph in  a word document
    Code snippet for Presentation:
    if (slide.Shapes[i].TextFrame2.HasText == MsoTriState.msoTrue)
                                {
                                    var langID = slide.Shapes[i].TextFrame2.TextRange.LanguageID;
                                    langList.Add(langID.ToString());
                                }
    The above code gives the language of the text which is present in a shape.
    The issue is with excel files. Can anyone please guide me on how to detect the language of the contents in an excel file?
    Also correct me if any anything wrong with word and presentation code posted above.

    Thanks in advance!

    Tuesday, November 15, 2016 6:42 AM
  • Hi, Jegadheswari

    I suggest you create your own thread for the issue about Excel:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=exceldev

    Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 17, 2016 4:49 AM
    Moderator