وقت بخیر دوستان.
من با به کمک یکی از تایپیکهای سایت کد زیر رو برای میل مرج ایجاد کردم. اما مشکل اینه که داده ها جایگزین نمیشه!!
ممکنه مشکل از ورد باشه؟ورد من 2013 هست . کد هم به صورت زیر هست
//THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE;
Object oTemplatePath = string.Format("{0}\\NewDocument.dot", Application.StartupPath);
//OBJECT OF MISSING "NULL VALUE"
Object oMissing = System.Reflection.Missing.Value;
//OBJECTS OF FALSE AND TRUE
Object oTrue = true;
Object oFalse = false;
//CREATING OBJECTS OF WORD AND DOCUMENT
Microsoft.Office.Interop.Word.Application oWord = null;
Microsoft.Office.Interop.Word.Document oWordDoc = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String FilePath = "C:\\Users\\6575\\Desktop\\A5.docx";
// تبدیل فایل به آرایه بایتی
byte[] FileBuffer = System.IO.File.ReadAllBytes(FilePath);
// حالا این آرایه رو داخل دیتابیس درج کن
System.IO.File.WriteAllBytes(oTemplatePath.ToStrin g(), FileBuffer);
oWord = new Microsoft.Office.Interop.Word.Application();
oWordDoc = new Microsoft.Office.Interop.Word.Document();
//Adding A New Document From A Template
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
int iTotalFields = 0;
// Finding Mailmerge Fields
foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields)
{
iTotalFields++;
Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
// Only Get The Milmerge Fields
if (fieldText.StartsWith(" MERGEFIELD"))
{
// Gives The Fieldnames as Entered in .DOT File
string fieldName = fieldText.Substring(12, fieldText.IndexOf(" ", 12) - 12);
switch (fieldName)
{
case "Date":
myMergeField.Select();
oWord.Selection.TypeText(textBox1.Text);
break;
case "Id":
myMergeField.Select();
oWord.Selection.TypeText(textBox2.Text);
break;
// And So On
default:
break;
}
}
}
//Showing The Document To The User
oWord.Visible = true;
}