我找到了一种方法使用书签就像曼努埃尔说做到这一点:
string[] readText = File.ReadAllLines(@"p:\CARTAP1.txt");
// declare objects and variables
object fileName = @"P:\PreciboCancelado.doc";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
// create instance of Word
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
// Create instance of Word document
Microsoft.Office.Interop.Word.Document oWordDoc = new Document();
// Open word document.
oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly,
ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
oWordDoc.Activate();
// Debug to see that I can write to the document.
oWordApp.Selection.TypeText("This is the text that was written from the C# program! ");
oWordApp.Selection.TypeParagraph();
// Example of writing to bookmarks each bookmark will have exists around it to avoid null.
if (oWordDoc.Bookmarks.Exists("Fecha"))
{
// set value for bookmarks
object oBookMark = "Fecha";
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[2] ;
oBookMark = "Nombre";
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[3];
}
// Save the document.
oWordApp.Documents.Save(ref missing, ref missing);
// Close the application.
oWordApp.Application.Quit(ref missing, ref missing, ref missing);
[如何使用C#插入字符到文件(HTTP的可能重复://计算器.com/questions/98484/how-to-insert-characters-to-a-file-using-c-sharp) – meJustAndrew
你必须阅读漏洞文档并重新写入,并添加新文本。有人问这[这里](http://stackoverflow.com/questions/98484/how-to-insert-characters-to-a-file-using-c-sharp),并得到了类似的答案。 – meJustAndrew
我对Word文档不太熟悉,但如果是我,我会利用Intellisence和/或[调试器](http://www.codeproject.com/Articles/79508/Mastering-Debugging -in-Visual-Studio-A-Beginn),并查看在“文档”中找到的属性和方法。您可能要编辑“Document”对象并保存它,覆盖现有文件。 –