我有一个带桌子的预制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;
希望这可以帮助...
关于c# - 如何使用C#将表格粘贴到Ms-Word文档的末尾,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6171623/