我创建了一个包含字段占位符的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 我知道我可以关闭进程whit代码[ process.Kill()],但我不知道应该杀死哪个进程。 如果我想用名称[WINWORD.exe]杀死所有进程,所有Word窗口都关闭。但我想关闭特定的Word窗口并终止我打开的进程。
怎么做?
答案 0 :(得分:2)
如果Quit方法无法帮助检查出来(只需用winword替换excel对象):http://blogs.msdn.com/b/msdnforum/archive/2010/03/09/excel-does-not-quit-after-automation-from-net-side.aspx
编辑: 和核心解决方案:
public static class WinWordKiller
{
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern long GetWindowThreadProcessId(long hWnd, out long lpdwProcessId);
public static void Kill(ref Microsoft.Office.Interop.Word.Application app)
{
long processId = 0;
long appHwnd = (long)app.Hwnd;
GetWindowThreadProcessId(appHwnd, out processId);
Process prc = Process.GetProcessById((int)processId);
prc.Kill();
}
}
答案 1 :(得分:0)
为什么不将Microsoft.Office.Interop.Word.Application.Quit与您已打开的应用程序(WordApp)一起使用?
我应该注意这样做,你应该当然坚持你的WordApp参考,并且在你打电话退出之前不要取消它。