private void VelizinBelDok() { try { System.Threading.Thread.Sleep(100); Cursor.Current = Cursors.WaitCursor; int iTotalFields = 1; 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 (var cmd = new OleDbCommand("Select * from gezilistemiz25,gezibilgileri25 WHERE unvani='Öğrenci'", conn)) { using (OleDbDataReader dr = cmd.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { 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); 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(); //cmd.Dispose(); //conn.Dispose(); oWord.ActiveDocument.SaveAs2(Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Veli İzin 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 İzin Belgesi Word formatında masaüstüne kaydedildi!"); } } } } } catch (Exception hata) { MessageBox.Show("İşlem Sırasında Hata Oluştu." + hata.Message); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)