How to paste table to the end of a Ms-Word documen

2020-07-24 04:40发布

问题:

I have a pre-made Word Template that has a table. I would like to open it up and then add (paste) another table at the end of the document. The problem is that it will not goto the end of the document, instead it paste the new table into the first cell of the original table. Any help would be greatly appreciated.

//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);

回答1:

Change the following line of your code

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

to

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

Hope this helps...



回答2:

I have found the answer!!

Instead of using Word.Range.Paste I used the following:

          Object objUnit = Word.WdUnits.wdStory;

          oWord.Selection.EndKey(ref objUnit, ref oMissing);

          oWord.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);


回答3:

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;
}