I want to fill out the form below by sequentially pulling the data from the database with Word Automation. A separate form will be filled out for each person. My codes pulls the same person. What's the problem?
This is the result.
This is the thing I want to do :
My codes are here :
private void VelizinBelDok() { try { System.Threading.Thread.Sleep(100); Cursor.Current = Cursors.WaitCursor; int iTotalFields = 0; 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; oWord.WindowState = Word.WdWindowState.wdWindowStateMinimize; oWord.WindowState = Word.WdWindowState.wdWindowStateMaximize; Object oTemplatePath = System.Windows.Forms.Application.StartupPath + "\\Belgeler\\VelizinBel.docx"; oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); //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); using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = gezievrak2541.accdb; Jet OLEDB:Database Password = Fatih2541; Mode = ReadWrite")) { conn.Open(); using (var cmd = new OleDbCommand("Select * from gezilistemiz25,gezibilgileri25 WHERE unvani='Ögrenci'", conn)) { using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { foreach (Word.Field myMergeField in oWordDoc.Fields) { 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); fieldName = fieldName.Trim(); if (fieldName == "sinifi") { myMergeField.Select(); oWord.Selection.TypeText(dr["sinifi"].ToString()); } if (fieldName == "ono") { myMergeField.Select(); oWord.Selection.TypeText(dr["ono"].ToString()); } if (fieldName == "adi") { myMergeField.Select(); oWord.Selection.TypeText(dr["adi"].ToString()); } if (fieldName == "soyadi") { myMergeField.Select(); oWord.Selection.TypeText(dr["soyadi"].ToString()); } if (fieldName == "gtarihi") { myMergeField.Select(); oWord.Selection.TypeText(DateTime.Parse(dr["gtarihi"].ToString()).ToShortDateString()); } if (fieldName == "dtarihi") { myMergeField.Select(); oWord.Selection.TypeText(DateTime.Parse(dr["gezilistemiz25.dtarihi"].ToString()).ToShortDateString()); } if (fieldName == "gyeri") { myMergeField.Select(); oWord.Selection.TypeText(dr["gyeri"].ToString()); } iTotalFields++; } } oWordDoc.Activate(); oWord.Activate(); } } conn.Close(); cmd.Dispose(); conn.Dispose(); //GC.WaitForPendingFinalizers(); //GC.Collect(); } oWord.ActiveDocument.SaveAs2(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Veli Izin Belgesi.docx")); oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing); oWord.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord); oWordDoc = null; oWord = null; //GC.WaitForPendingFinalizers(); //GC.Collect(); Cursor.Current = Cursors.Default; MessageBox.Show("Veli Izin Belgesi Word formatinda masaüstüne kaydedildi!"); } } catch (Exception hata) { MessageBox.Show("Islem Sirasinda Hata Olustu." + hata.Message); } }