user1059110
user1059110

Reputation: 129

insert at bookmark

I have the following method(not my program) that inserts table data to the end of the document. I want to insert the data into a bookmark in the document. How do I reference that bookmark instead of the \endofdoc?

    private static void CreateTable(Microsoft.Office.Interop.Word.Document oWordDoc, int RowCount, int ColumnCount, string[,] TableContent)
    {
        Table oTable;
        object oEndOfDoc = "\\endofdoc";
        object missing = System.Reflection.Missing.Value;
        Range wrdRng = oWordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
        oTable = oWordDoc.Tables.Add(wrdRng, RowCount, ColumnCount, ref missing, ref missing);


        oTable.ID = "ContentTable";
        int r, c;


        for (r = 0; r < RowCount; r++)
            for (c = 0; c < ColumnCount; c++)
            {
                oTable.Cell(r + 1, c + 1).Range.Text = TableContent[r, c];
            }


        //oTable.Rows[1].Range.Font.Bold = 1;

        oTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
        oTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    }

Upvotes: 0

Views: 6377

Answers (1)

Josh
Josh

Reputation: 10624

The following code was culled from c-sharpcorner which should get you started with dealing with bookmarks.

//BOOK MARK FOR START OF SELECTION

Object oBookmarkStart = "BookMark__Start";

Object oRngoBookMarkStart = oWordDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart).Range.Start;



//BOOK MARK FOR END OF SELECTION

Object oBookmarkEnd = "BookMark__End";

Object oRngoBookMarkEnd = oWordDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoEnd).Range.Start;



//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS

Word.Range rngBKMarkSelection = oWordDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);



//SELECTING THE TEXT

rngBKMarkSelection.Select();
rngBKMarkSelection.Delete(ref oMissing, ref oMissing);

Upvotes: 1

Related Questions