我在特定位置添加了书签并使用 .dotx 保存该文件。当我运行解决方案时,它会在模板文件中添加日期,但会丢失模板中存储的所有剩余记录。我想在工作模板中动态添加数据,并且数据可以增长它会有所不同。
object oMissing = Missing.Value;
string DateParameter = DateTime.Today.Date.ToString();
Word._Application word = new Word.Application();
word.Visible = false;
// Template
object oTemplate = @"..\SampleTemplate.dotx";
Word._Document document = word.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
object oBookMark = "DateParameter";
Word.Range wrdRng = document.Bookmarks.get_Item(oBookMark).Range;
Microsoft.Office.Interop.Word.Range rng = document.Content;
rng.Text = DateParameter;
document.SaveAs2("TestDocument" + DateTime.Now.Millisecond, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
GC.Collect();
谢谢