VSTO字加载项:set_Style(参考代码),忽略背景颜色

问题描述 投票:0回答:1

当我尝试设置选择代码风格与样式:

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

它忽略了背景色(lightblue):

enter image description here

当我把它应用到的范围内,它会为整款:

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

enter image description here

定义的“代码”是:

enter image description here

我只希望在代码风格字/选择“攻击”。我究竟做错了什么?当我录制一个宏它给了我这样的:

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

但是这并不能帮助我太多...

c# ms-word vsto office-addins
1个回答
0
投票

我发现这个问题。您可以附加一个样式后的风格基础:

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);
© www.soinside.com 2019 - 2024. All rights reserved.