Mehmet Fatih

Mehmet Fatih

  • 859
  • 941
  • 42.7k

Error in loop action with word automation using C#

Jun 15 2023 7:03 AM

I am trying to export data from database to word document. But it breaks in the loop action. How can we loop multiple rows? It works very well without loop. But when it loops, it gives error.

Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();

oWord.Visible = true;
Object oTemplatePath = System.Windows.Forms.Application.StartupPath + "\\GeziListesi.docx";
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

foreach(Word.Field myMergeField in oWordDoc.Fields) {
    iTotalFields++;

    Word.Range rngFieldCode = myMergeField.Code;
    String fieldText = rngFieldCode.Text;

    if (fieldText.StartsWith(" MERGEFIELD")) {
        Int32 endMerge = fieldText.IndexOf("\\");
        Int32 fieldNameLength = fieldText.Length - endMerge;
        String fieldName = fieldText.Substring(11, endMerge - 11);

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=gezievrak2541.mdb; Mode = ReadWrite");

        if (conn.State == ConnectionState.Closed) {
            conn.Open();
        }

        using(OleDbCommand cmd = new OleDbCommand("Select * from kurumbilgi25, gezibilgileri25", conn)) {
            using(OleDbDataReader dr = cmd.ExecuteReader()) {
                while (dr.Read()) {

                    fieldName = fieldName.Trim();

                    if (fieldName == "kurumu") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["kurumbilgi25.kurumu"].ToString());
                    }
                    if (fieldName == "gtarihi") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(DateTime.Parse(dr["gtarihi"].ToString()).ToShortDateString());
                    }

                    if (fieldName == "tcno") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["tcno"].ToString());
                    }
                    if (fieldName == "adsoyad") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["adi"].ToString() + " " + dr["soyadi"].ToString());
                    }
                    if (fieldName != "sinifi") {
                        //myMergeField.Select();
                        oWord.Selection.TypeText(" ");

                    }
                    if (fieldName == ("sinifi")) {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["sinifi"].ToString());
                    }

                    if (fieldName == "unvan") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["gezilistemiz25.unvani"].ToString());
                    }
                    if (fieldName == "tarih") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(DateTime.Now.ToShortDateString());
                    }
                    if (fieldName == "mudur") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["mudur"].ToString());
                    }
                    if (fieldName == "unvan2") {
                        myMergeField.Select();
                        oWord.Selection.TypeText(dr["kurumbilgi25.unvani"].ToString());
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                }
            }
        }
    }
}

 


Answers (5)