سلام من براتون یه الگو میدماز روی این مشکلتون حل میشه
6 تا تسکت باکس و یه لیست باکس و یه باتن به فرم اضافه کنید
namespace ==>>Microsoft.Office.Interop.Excel اضافه کنید
این کدها رو هم درون فرم قرار بدید
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using Microsoft.Office;
using Word = Microsoft.Office.Interop.Word;
namespace CreateWord
{
public partial class frm_main : Form
{
int iTotalFields = 0;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
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+"\\Report.dot";
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 == "DATE")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_name.Text);
}
if (fieldName == "ORG")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_address.Text);
}
if (fieldName == "Name")
{
myMergeField.Select();
oWord.Selection.TypeText(num_age.Text);
}
if (fieldName == "Sender")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_email.Text);
}
if (fieldName == "Mail")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_company.Text);
}
if (fieldName == "Phone")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_tel.Text);
}
if (fieldName == "ODetails")
{
myMergeField.Select();
oWord.Selection.TypeText(txt_odetails.Text);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
}
}