【发布时间】:2012-04-12 08:45:53
【问题描述】:
我创建了一个带有一些占位符的 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 窗口并杀死我打开的进程。
怎么做?
【问题讨论】:
-
我认为对你来说最好的解决方案是找到线程的 id 并杀死它?
-
我会试试
Marshal.ReleaseComObject -
System.Runtime.InteropServices
标签: c# ms-word ms-office word-template