【问题标题】:Get Localized / Unlocalized style names in Word (VSTO)在 Word (VSTO) 中获取本地化/非本地化样式名称
【发布时间】:2014-01-12 12:24:32
【问题描述】:

我有一个单词插件,需要帮助处理样式名称。

我使用 get_Style().NameLocal 获得了段落样式。这将返回本地化名称,具体取决于 Office 运行时使用的语言。

只要有内置样式,我就找到了一种通过将 wdBuiltInStyle 应用于段落并阅读 Namelocal 来获取本地名称的方法。但是,大约有 134 种内置样式,而普通模板大约有 134 种内置样式。内部有 270 种款式。大多数不在枚举中的都是表格样式。

那么,问题是,我在哪里可以获得其他样式的英文(内部)名称,以确定这种样式在所有可能的语言中的用法?

这是一些伪代码,解释了我想要得到的东西(我正在寻找 GetEnglishName() 方法):

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

【问题讨论】:

    标签: ms-word vsto


    【解决方案1】:

    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;
    }
    

    这种方法只是简单地遍历所有样式,尝试一个一个地应用它,然后提取本地化名称并将其存储在字典中。加载文档后必须调用一次。因为内部样式没有公开获取样式类型的方法,所以我可以处理在无效位置写入的样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多