开发者

Replace text in Word document with headings

In a few word documents, I've placed a "variable" (ie: #VARIABLE). I'm pulling information from a database that I would like to put at this specific place, though I'm not sure how much information. I'm using the Word.Find.Replacement method for finding #VARIABLE and inserting the queried text there. The problem is that I would like to format that text so that it is of type Heading1, this way it will show up in my table of contents. How do I go about setting the heading? Here is a snippet of my code...

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdRe开发者_运维技巧placeOne;
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);

I have tried several things like this but it doesn't seem to work.

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


I solved this problem. You simply use...

tmpRange.set_Style(#your style);

The way I have this example though, it will apply the style to ALL of your find/replace texts. You will need to create a new range for each item selected if you want the styles to be different for each replacement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜