用标题替换Word文档中的文本

时间:2011-09-08 17:54:14

标签: c# ms-word

在一些单词文档中,我放置了一个“变量”(即:#VARIABLE)。我正在从我希望放在这个特定位置的数据库中提取信息,但我不知道有多少信息。我正在使用Word.Find.Replacement方法来查找#VARIABLE并在那里插入查询的文本。问题是我想格式化该文本,使其类型为Heading1,这样它将显示在我的目录中。我该如何设置标题?这是我的代码片段......

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
tmpRange.Find.Text = @"#HERE";
myReader.Read();

tmpRange.Find.Replacement.Text = myReader.GetValue(0) + System.Environment.NewLine + "\n #HERE";
tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;

tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);

我尝试过这样的几件事,但似乎没有用。

tmpRange.Find.Replacement.Font.set_style(Word.WdBuiltinStyle.wdStyleHeading1);

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。你只需使用......

tmpRange.set_Style(#your style);

我有这个例子的方式,它会将样式应用于所有查找/替换文本。如果您希望每次替换的样式不同,则需要为所选的每个项目创建一个新范围。

相关问题