【问题标题】:VSTO Word Addin: set_Style(ref code), ignoring background colorVSTO Word Addin:set_Style(参考代码),忽略背景颜色
【发布时间】:2019-02-06 15:03:39
【问题描述】:

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

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

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

当我将它应用到 Range 时,它会为整个段落执行此操作:

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

定义的“代码”是:

我只想要代码风格的单词/选择“攻击者”。我做错了什么?当我录制一个宏时,它给了我这个:

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

但这对我没有多大帮助......

【问题讨论】:

  • 您能否使用问题下方的edit 链接来包含有关样式定义的信息?另外,这是字符样式、段落样式还是链接样式类型?
  • 我无法复制,但是... Range 对象是什么?一个选择?一个段落...?在应用样式之前这个范围有什么格式?
  • 感谢您的提问。我有不同的问题,无论是选择还是段落......见上文。我编辑了我的问题。

标签: c# ms-word vsto office-addins


【解决方案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 Char"; //appending Char made its job!
currentSelection.Range.set_Style(ref codeStyle);

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 2015-02-20
    • 2023-03-26
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多