Automate Word in C # - Create Tables in Word

I have a C # application that will open a Word document and then replace some of the predefined bookmarks with data that I have looks like Name, Class, etc.

These are all just string values. Now I want to display a table with a dynamic number of rows in the word document.I want the table to be at a specific location in the document.

Can I use a bookmark for this. If so, how ??? Is there any other method?

0


source to share


1 answer


Yes, you can use bookmarks and also use fields to replace it with a table with n no. lines and n not. columns.

You can iterate over the fields and get your range and use the range you can add to the table in place:



//CREATING OBJECTS OF WORD AND DOCUMENT

Word.Application oWord = new Word.Application();

Word.Document oWordDoc = new Word.Document();

foreach (Word.Field myMergeField in oWordDoc.Fields)

{

    iTotalFields++;

    Word.Range rngFieldCode = myMergeField.Code;

    String fieldText = rngFieldCode.Text;    


    // ONLY GETTING THE MAILMERGE FIELDS    
    if (fieldText.StartsWith("tablename"))

    {
        myMergeField.Select();
        oWordDoc.table.add(rngFieldCode,4//for rows,4// for colioulns,ref omising....);
     }
}

      

+3


source







All Articles