如何使用C#在Word文档的底部添加html文本。我已经在Word文档中添加了html文本,但它始终附加在文档顶部。
private static void WordApp_DocumentOpenEvent(Document Doc)
{
string html = "<div><h3>${}</h3></div>";
object missing = Type.Missing;
ContentControl contentControl = Doc.ContentControls.Add(WdContentControlType.wdContentControlRichText);
contentControl.Title = "This is html content";
int start = Doc.Characters.Count - 1;
object end = start;
Range rng = Doc.Range(start, end);
rng.Select();
contentControl.Range.Move(3);
contentControl.Range.InsertFile(SaveToTemporaryFile(html));
}
public static string SaveToTemporaryFile(string html)
{
string htmlTempFilePath = Path.Combine(Path.GetTempPath(), string.Format("{0}.html", Path.GetRandomFileName()));
using (StreamWriter writer = File.CreateText(htmlTempFilePath))
{
html = string.Format("<html>{0}</html>", html);
writer.WriteLine(html);
}
return htmlTempFilePath;
}
InsertFile已经具有range参数,但是我不知道它的工作方式,或者我没有适合此方法的文档。