在Word(VSTO)中获取本地化/未本地化的样式名称 [英] Get Localized / Unlocalized style names in Word (VSTO)
问题描述
我有一个外接词,需要有关样式名称的帮助.
I have a word Add-In and need help dealing with style names.
我使用get_Style().NameLocal获得段落样式.这将返回本地化的名称,具体取决于Office运行所使用的语言.
I get a paragraphs style with get_Style().NameLocal. This returns the localized name, depending on the language Office runs with.
只要有内置样式,我就找到了一种通过将wdBuiltInStyle应用于段落并读取Namelocal来获取本地名称的方法.但是,大约有134种内置样式,而常见的模板大约有30种样式.内部有270种样式.大部分不在枚举中的是表格样式.
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?
这是一些伪代码,解释了我要获取的内容(我正在寻找GetEnglishName()方法):
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);
}
推荐答案
2014年1月更新
我自己编写了该函数.不确定这是否是最好的方法,因此请检查一下并发表评论.
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.
这篇关于在Word(VSTO)中获取本地化/未本地化的样式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!