我将文本插入Word文档。这是由selection.TypeText("text");
我想将格式化文本插入Word文档,例如:
public override void InsertText(string content, string format)
{
selection.Style = format; //something like this
selection.Font.Name = "Heading 1"; //or like this
selection.TypeText(content);
}
有什么想法吗?
答案 0 :(得分:1)
对于引用Microsoft.Office.Interop.Word
的Word文档级自定义,这适用于:
this.ActiveWindow.Selection.Range.Font.Name = "Arial";
this.ActiveWindow.Selection.Range.Font.Size = 36;
您还可以将选区范围指定给范围变量,然后将格式应用于变量,如下所示:
Word.Range myRange = this.ActiveWindow.Selection.Range;
myRange.Font.Size = 18;
myRange.Font.Name = "Arial";
编辑(评论中回答OP的问题)
要将标题样式应用于所选文本,请指定Word的WdBuiltinStyle
枚举成员之一:
object headingStyle = Word.WdBuiltinStyle.wdStyleHeading1;
this.ActiveWindow.Selection.Range.set_Style(ref headingStyle);
要查看枚举成员的完整列表,请参阅: