ฉันเขียนโปรแกรม Windows Forms โดยใช้ C# ประกอบด้วยกล่องข้อความและตารางข้อมูล
ฉันต้องการส่งออกข้อมูลเป็นไฟล์ Word เพื่อใช้เทมเพลต ฉันเพิ่มไลบรารีของ Microsoft Word ลงในโค้ดของฉัน
นอกจากนี้ ฉันยังสร้างกล่องข้อความ "รูปร่าง" ภายในไฟล์ Word และใส่ข้อความไว้ข้างในเพื่อค้นหาจากโปรแกรม c# ของฉัน
สำหรับตอนนี้ ทุกอย่างดูโอเค แต่ฉันติดอยู่ที่ฝั่ง C# ฉันสามารถแทนที่ข้อความปกติได้ แต่ไม่ใช่ข้อความที่อยู่ในกล่องข้อความ "Inside the word file"
และนี่คือรหัสของฉัน:
private void FindAndReplace(Word.Application wordapp, object ToFindText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object matchAllforms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object matchread_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordapp.Selection.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref matchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
private void CreateWordDocument(object filename, object SaveAs)
{
Word.Application wordApp = new Word.Application();
object missing = Missing.Value;
Word.Document myWordDoc = null;
if (File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
myWordDoc = 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 missing,
ref missing, ref missing, ref missing, ref missing);
myWordDoc.Activate();
//find and replave
this.FindAndReplace(wordApp, "<date1>", textBox2.Text);
this.FindAndReplace(wordApp, "<date2>", textBox3.Text);
}
else
{
MessageBox.Show("File not found!");
}
//Svae as
myWordDoc.SaveAs2(ref SaveAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
myWordDoc.Close();
wordApp.Quit();
MessageBox.Show("File Created!");
}