C# 操作Word出现多了winword.exe的解决方案

 1 using Word = Microsoft.Office.Interop.Word;
2
3 // 创建Word文档
4 Word.Application WordApp = null;
5 Word.Document WordDoc = null;
6
7 Object Nothing = System.Reflection.Missing.Value;
8 WordApp = new Word.ApplicationClass();
9 WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
10
11 // Word文件路径
12 string filename = System.Windows.Forms.Application.StartupPath + "\\test.doc";
13 string message = "";
14
15 try
16 {
17 // 创建Word文档
18 // ... ...
19 }
20 catch (Exception ex)
21 {
22 MessageBox.Show(message = "文件导出异常!" + ex.ToString());
23 }
24 finally
25 {
26 //文件保存
27 WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
28 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
29
30 // Solution-1
31 object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
32 WordApp.Quit(ref saveOption, ref Nothing, ref Nothing);
33 }

解决方案 二

 1             try
2 {
3 if (WordDoc != null)
4 {
5 System.Runtime.InteropServices.Marshal.ReleaseComObject(WordDoc);
6 WordDoc = null;
7 }
8 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
9 }
10 catch
11 {
12 try
13 {
14 if (WordApp != null)
15 {
16 System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
17 WordApp = null;
18 }
19 }
20 catch (Exception ex1)
21 {
22 MessageBox.Show(ex1.ToString());
23 }
24
25 }



posted on 2011-11-10 13:45  LuckyLuke 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/LukeChen/archive/2011/11/10/2244370.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值