如何使用C#将表粘贴到Ms-Word文档的末尾

我有一个预制的Word模板,有一张桌子。 我想打开它,然后在文档的末尾添加(粘贴)另一个表。 问题是它不会转到文档的末尾,而是将新表粘贴到原始表的第一个单元格中。 任何帮助将不胜感激。

//previous code copied a table from another document Object oTempPath = "C:\\Temp\\Logtemp.doc"; Object defaultTemplate = "C:\\Temp\\LogContemp.doc"; oDoc = oWord.Documents.Open(ref defaultTemplate, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = oDoc.Content.Start; object end = oDoc.Content.End; oDoc.Range(ref start, ref end).Copy(); oDoc.Close(ref oMissed, ref oMissed, ref oMissed); oDoc = oWord.Documents.Open(ref oTempPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.Activate(); //**** This is where the issue starts **** start = oWord.Selection.End; end = oWord.Selection.End; Word.Range rng = oDoc.Range(ref start, ref end); rng.Select(); rng.Paste(); object fileN1 = "C:\\temp\\" + TextBox1.Text + " Log.doc"; oDoc.Fields.Update(); oDoc.SaveAs(ref fileN1, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed, ref oMissed); oDoc.Close(ref oMissed, ref oMissed, ref oMissed); oWord.Quit(ref oMissing, ref oMissing, ref oMissing); 

更改代码的以下行

 start = oWord.Selection.End; end = oWord.Selection.End; 

 start = oDoc.Content.End - 1; end = oDoc.Content.End; 

希望这可以帮助…

我找到了答案!!

而不是使用Word.Range.Paste我使用以下内容:

  Object objUnit = Word.WdUnits.wdStory; oWord.Selection.EndKey(ref objUnit, ref oMissing); oWord.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault); 
 Globals.ThisAddIn.Application.ActiveDocument.Range( Globals.ThisAddIn.Application.ActiveDocument.Content.End-1, Globals.ThisAddIn.Application.ActiveDocument.Content.End-1).Select(); object missing = System.Type.Missing; Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add( currentRange, 3, 4, ref missing, ref missing); // Get all of the borders except for the diagonal borders. Word.Border[] borders = new Word.Border[6]; borders[0] = newTable.Borders[Word.WdBorderType.wdBorderLeft]; borders[1] = newTable.Borders[Word.WdBorderType.wdBorderRight]; borders[2] = newTable.Borders[Word.WdBorderType.wdBorderTop]; borders[3] = newTable.Borders[Word.WdBorderType.wdBorderBottom]; borders[4] = newTable.Borders[Word.WdBorderType.wdBorderHorizontal]; borders[5] = newTable.Borders[Word.WdBorderType.wdBorderVertical]; // Format each of the borders. foreach (Word.Border border in borders) { border.LineStyle = Word.WdLineStyle.wdLineStyleSingle; border.Color = Word.WdColor.wdColorBlue; }