gpt4 book ai didi

ms-word - 在 Word (VSTO) 中获取本地化/未本地化的样式名称

转载 作者:行者123 更新时间:2023-12-03 19:44:10 24 4
gpt4 key购买 nike

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

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

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

所以,问题是,我在哪里可以获得附加样式的英文(内部)名称,以确定所有可能的语言是否使用这种样式?

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

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

最佳答案

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

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

关于ms-word - 在 Word (VSTO) 中获取本地化/未本地化的样式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com