Shyju
Shyju

Reputation:

Word Automation in C#- Creating Tables in Word

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

It is all are just string values .Now I want to render a Table with dynamic number of rows to the word document.I want the table in a particular place in the document.

Can i use bookmark for this.If so how ??? IS there any other method?

Upvotes: 0

Views: 3780

Answers (1)

paresh koyani
paresh koyani

Reputation:

Yes, you can use bookmarks and also use fields to replace it with table with n no. of rows and n no. of columns.

You can loop through fields and get its range and using range you can add table at field location:

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

Upvotes: 3

Related Questions