2014-01-12 59 views
3

我有一個單詞插件並需要幫助處理樣式名稱。在Word中獲取本地化/非本地化樣式名稱(VSTO)

我使用get_Style()。NameLocal獲得段落樣式。這將返回本地化的名稱,具體取決於Office運行的語言。

只要有內置樣式,我找到了一種方法來獲取本地名稱,方法是將wdBuiltInStyle應用於段落,然後讀取Namelocal。但是,大約有134種內置樣式,而通用模板大約有。內部270種風格。大多數不在枚舉中的是表格樣式。

所以,問題是,我在哪裏可以得到附加樣式的英文(內部)名稱,以確定這種樣式適用於所有可能的語言?

這是一些僞代碼,以解釋什麼,我試圖讓(我正在尋找一個GetEnglishName()方法):

foreach (Style wd in CurrentDocument.Styles) { 
    _defaultStyleNames.Add(**GetEnglishName(wd)**, wd.NameLocal); 
} 

回答

0

更新2014年1月

我有由我自己寫的功能。不確定這是否是最好的方式,所以請檢查並評論。

private readonly IDictionary<WdBuiltinStyle, string> _localStyleNames = new Dictionary<WdBuiltinStyle, string>(); 
private readonly IDictionary<Style, string> _defaultStyleNames = new Dictionary<Style, string>(); 

private string GetLocalizedName(WdBuiltinStyle wd) { 
    return _localStyleNames[wd]; 
} 

private string GetLocalizedName(Style wd) { 
    return _defaultStyleNames[wd]; 
} 
    internal bool CheckLocalizedStyleName(Style style, WdBuiltinStyle wd) { 
    var styleName = style.NameLocal; 
    return GetLocalizedName(wd).Contains(styleName); 
} 

private void LocalizedNames() { 
    Globals.ThisAddIn.Application.ScreenUpdating = false; 
    if (!_localStyleNames.Any()) { 
    // create a test object 
    // Move this to start up routine to get localized names 
    foreach (var wd in Enum.GetValues(typeof (WdBuiltinStyle))) { 
     object s = (WdBuiltinStyle) wd; 
     Paragraph testPar = null; 
     try { 
      testPar = CurrentDocument.Paragraphs.Add(); 
     testPar.set_Style(ref s); 
     var headingStyle = (Style) testPar.get_Style(); 
       CurrentDocument.Paragraphs[CurrentDocument.Paragraphs.Count].Range.Delete(); 
     var headingStyleName = headingStyle.NameLocal; 
      _localStyleNames.Add((WdBuiltinStyle) s, headingStyleName); 
     } 
     catch (Exception ex) { 
      Range testRange = null; 
      // not a para style, trying range style 
      try { 
       testRange = testPar.Range; 
       testRange.set_Style(ref s); 
       var rangeStyle = (Style) testRange.get_Style(); 
       var rangeStyleName = rangeStyle.NameLocal; 
       _localStyleNames.Add((WdBuiltinStyle) s, rangeStyleName); 
        CurrentDocument.Paragraphs[CurrentDocument.Paragraphs.Count].Range.Delete(); 
       } 
       catch (Exception) { 
        // even not range, try table 
        if (s.ToString().Contains("Table")) { 
         try { 
          var t = CurrentDocument.Tables.Add(testRange, 1, 1, null, null); 
          t.set_Style(ref s); 
          var tStyle = (Style) t.get_Style(); 
          var tStyleName = tStyle.NameLocal; 
          _localStyleNames.Add((WdBuiltinStyle) s, tStyleName); 
          t.Delete(); 
         } 
         catch (Exception) { 
         // ignore even this 
        } 
        } 
       } 
      } 
      finally { 
       if (testPar != null) { 
        testPar.Range.Delete(); 
       } 
      } 
     } 
    } 
    if (!_defaultStyleNames.Any()) { 
    // after this, all built in names are present. However, word has some more styles "embedded" and those we catch here 
     foreach (Style wd in CurrentDocument.Styles) { 
      _defaultStyleNames.Add(wd, wd.NameLocal); 
     } 
    } 
    Globals.ThisAddIn.Application.ScreenUpdating = true; 
} 

這種方法只是簡單地通過所有樣式,嘗試逐個應用它,並提取本地化名稱並將其存儲在字典中。加載文檔後需要調用一次。因爲內部樣式不公開獲取樣式的方式,所以我使用異常來處理寫在無效位置的樣式。