0
Answer

Filling a predefined table in word template by C#

Maroof Raini

Maroof Raini

10y
1.1k
1
I am trying to create a bill say a mobile customized bill, to make it good looking I am using word template and merge fields to fill the values occuring once only, like customer name, bill period, date etc. but to show customized bill I need to populate the bill detail in the same word doc.
Somehow like this

<<CustomerName>><MobileNumber>><<BillPeriod>> <<Date>>
NumberDurationCall(s)Cost









<<Company Name>>
<Address>>

I am able to replace the fields CustomerName, MobileNumber, BillPeriod, Date by using below code
  foreach (Word.Field myMergeField in myWordDoc.Fields)
                {
                    Word.Range rngFieldCode = myMergeField.Result;
                    String fieldText = rngFieldCode.Text;
                    if (fieldText.StartsWith(" MERGEFIELD"))
                    {
                        Int32 endMerge = fieldText.IndexOf("\\");
                        Int32 fieldNameLength = fieldText.Length - endMerge;
                        String fieldName = fieldText.Substring(11, endMerge - 11);                     
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (fieldName == dc.ColumnName)
                            {
                                myMergeField.Select();
                                myWordApp.Selection.TypeText(dt.Rows[0][dc.ColumnName].ToString());
                                
                            }
                        }

                    }
                }
But not able to fill the data in datatable, can someone please help me to fill the data in the table, please help me with C# code and the format I need to place in template file. Please avoid suggestion for tools like aspose etc.