フィールド用のいくつかのプレースホルダーを使用してWordTemplateを作成します。コードでは、このプレースホルダーに値を挿入してユーザーに表示します。
protected void Button1_Click(object sender, EventArgs e)
{
string DocFilePath = "";
//string FilePath = System.Windows.Forms.Application.StartupPath;
object fileName = @"[...]\asset\word templates\FormatPeygiri1.dot";
DocFilePath = fileName.ToString();
FileInfo fi = new FileInfo(DocFilePath);
if (fi.Exists)
{
object readOnly = false;
object isVisible = true;
object PaperNO = "PaperNO";
object PaperDate = "PaperDate";
object Peyvast = "Peyvast";
object To = "To";
object ShoName = "ShoName";
object DateName = "DateName";
Microsoft.Office.Interop.Word.Document 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 isVisible, ref missing, ref missing, ref missing);
WordApp.ActiveDocument.FormFields.get_Item(ref PaperNO).Result = TextBox_PaperNO.Text;
string strPaperDate = string.Format("{0}/{1}/{2}", PersianDateTimeHelper.GetPersainDay(DateTimePicker_PaperDate.SelectedDate),
PersianDateTimeHelper.GetPersainMonth(DateTimePicker_PaperDate.SelectedDate),
PersianDateTimeHelper.GetPersainYear(DateTimePicker_PaperDate.SelectedDate));
WordApp.ActiveDocument.FormFields.get_Item(ref PaperDate).Result = strPaperDate;
WordApp.ActiveDocument.FormFields.get_Item(ref Peyvast).Result = TextBox_Peyvast.Text;
WordApp.ActiveDocument.FormFields.get_Item(ref To).Result = TextBox_To.Text; ;
WordApp.ActiveDocument.FormFields.get_Item(ref ShoName).Result = TextBox_ShoName.Text;
string strDateName = string.Format("{0}/{1}/{2}", PersianDateTimeHelper.GetPersainDay(DateTimePicker_DateName.SelectedDate),
PersianDateTimeHelper.GetPersainMonth(DateTimePicker_DateName.SelectedDate),
PersianDateTimeHelper.GetPersainYear(DateTimePicker_DateName.SelectedDate));
WordApp.ActiveDocument.FormFields.get_Item(ref DateName).Result = strDateName;
aDoc.Activate();
WordApp.Visible = true;
aDoc = null;
WordApp = null;
}
else
{
MessageBox1.Show("File Not Exist!");
}
それはうまく機能します!ただし、ユーザーがWordを閉じると、プロセスは閉じられず、タスクマネージャーのプロセスリストに存在します。このプロセス名はWINWORD.exeです。プロセスの聖霊降臨祭のコード[ process.Kill() ]を閉じることができることは知っていますが、どのプロセスを強制終了する必要があるかわかりません。[WINWORD.exe]という名前のすべてのプロセスを強制終了する場合すべてのWordウィンドウを閉じますが、特定のWordウィンドウを閉じて、開いたプロセスを強制終了します。
どうやってするの?