PDA

View Full Version : چطور میتونم یه متن به فایل Word اضافه کنم



alinaghiha
جمعه 29 شهریور 1392, 09:50 صبح
من میخوام یه نرم افزار واسه نامه نگاری بنویسم
تا اینجا میتونم فایل word رو باز و ذخیره کنم حالا میخوام مثلا عنوان نامه رو تو همون فایل Word بنویسم mailmerge که به کارم نمیاد
چطور میتونم این کارو انجام بدم

alinaghiha
جمعه 29 شهریور 1392, 18:37 عصر
دوستان لطفا راهنمایی کنید

omid_student
یک شنبه 26 آبان 1392, 13:01 عصر
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace Bank_Reporter
{
class modifyWord
{
public void OpenWord(string sPath,string[] keys,string[] values)
{
try
{
object missing = Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
// create Word document object
Microsoft.Office.Interop.Word.Document aDoc = null;
object filename = sPath;
// if temp.doc available
if (File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
// make visible Word application
wordApp.Visible = false;
// open Word document named temp.doc
aDoc = wordApp.Documents.Open(ref filename, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
aDoc.Activate();
// Call FindAndReplace()function for each change
for (int i = 0; i <= keys.Length - 1; i++)
{
this.FindAndReplace(wordApp, keys[i], values[i]);
}
// save temp.doc after modified
aDoc.Save();
}
}
catch (Exception)
{
MessageBox.Show("Error in process.", "Internal Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void FindAndReplace(Microsoft.Office.Interop.Word.Appli cation wordApp,
object findText, object replaceText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText, ref matchCase,
ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
ref matchAllWordForms, ref forward, ref wrap, ref format,
ref replaceText, ref replace, ref matchKashida,
ref matchDiacritics,
ref matchAlefHamza, ref matchControl);
}

}
}