Mehmet Fatih

Mehmet Fatih

  • 798
  • 923
  • 32.2k

Pulling word automation into a single document

Oct 9 2023 6:37 PM

I want to fill out my students’ parental consent forms using the word automation method by pulling data from the database. In my code, I can only import one student's parental permission form into each word document. All I want is to pull data for each person one after the other on the same word document.

private void VelizinBelDok()
 {
     try
     {
         System.Threading.Thread.Sleep(100);
         Cursor.Current = Cursors.WaitCursor;

         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";
         

         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 (OleDbCommand cmd = new OleDbCommand("SELECT * FROM gezilistemiz25, gezibilgileri25 where unvani='Ögrenci'", conn))
             using (OleDbDataReader dr = cmd.ExecuteReader())
             {
                    while (dr.Read())
                 {
                     oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

                     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());
                                     }
                                  }
                     }
                     oWordDoc.Activate();
                    oWord.Activate();
                         }
                         conn.Close();
                         conn.Dispose();
                     }
                     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);
     }
 }

 


Answers (2)