Question

I have a word Add-In and need help dealing with style names.

I get a paragraphs style with get_Style().NameLocal. This returns the localized name, depending on the language Office runs with.

As long as there are Built in styles, I found a way to get the local names by applying wdBuiltInStyle to a paragraph and read Namelocal, then. However, there are roughly 134 built in styles, whereas a common template has approx. 270 styles internally. Most of those not in the enum are table styles.

So, question is, where can I get the English (internal) names of the additional styles to determine the usage of such a style for all prossible languages?

This is some pseudo-code that explains what I'm trying to get (I'm looking for a GetEnglishName() method):

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

Solution

Update Jan 2014

I have written the function by myself. Not sure that this is the best way ever, so please check it out and comment.

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

This method is simply going through all styles, tries to apply it one by one, and extract the localized name and stores it in a dictionary. It's necessary to call once after a document has been loaded. Because the internal styles do not expose a way to get the styles type I work with exception to handle styles written at invalid positions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top