我找到了一種方法使用書籤就像曼努埃爾說做到這一點:
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」對象並保存它,覆蓋現有文件。 –