VSTO Word 插件:set_Style(参考代码),忽略背景颜色

VSTO Word Addin: set_Style(ref code), ignoring background color

当我尝试将选择的样式设置为代码样式时:

Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Microsoft.Office.Interop.Word.Selection currentSelection = oWordApp.Selection;

object codeStyle = "Code";
// this disrespects the background
currentSelection.set_Style(ref codeStyle);

它忽略背景颜色(浅蓝色):

当我将它应用到 范围 时,它会在整个段落中应用:

Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Microsoft.Office.Interop.Word.Selection currentSelection = oWordApp.Selection;

// this sets the style to the whole paragraph, but i want the range only
currentSelection.Range.set_Style(ref codeStyle);

定义"Code"是:

我只想要 word/selection "attacker" 代码风格。 我做错了什么?当我录制一个宏时,它给了我这个:

Selection.Style = ActiveDocument.Styles("Code")

但这对我帮助不大...

我发现了问题。您可以在样式后附加样式库:

Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Microsoft.Office.Interop.Word.Selection currentSelection = oWordApp.Selection;

object codeStyle = "Code Char"; //appending Char made its job!
currentSelection.Range.set_Style(ref codeStyle);