注册 登录
编程论坛 C# 论坛

C#如何关闭word进程

lw19890506 发布于 2009-10-21 14:02, 5731 次点击
我在添加引用的com里面添加了word组件,也就是Microsoft Word 11.0 Object Library,然后用下面的代码打开电脑里面的word文件:
Word.Application app = new Word.Application();
Word.Document doc = null;

object fileName = url;
object missing = System.Reflection.Missing.Value;
object readOnly = true;
object isVisible = true;
object index = 0;

doc = app.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 missing,
    ref missing, ref missing, ref missing);
这样打开word文件之后,然后关闭程序。程序并没有终止word的进程,在任务管理器里的进程里总有个WINDOWS.EXE没有关闭。我试了好多方法都无法在程序里面关闭word进程。关不掉这个进程,原有的word文件就删不掉了,总是提示“无法关闭,另一程序正在使用...”。求高手指点!
(doc.Close();和app.Quit();都没用!)
4 回复
#2
baikil2009-10-21 15:56
可以去参考一下Execl组件退出方法,应该类似..
#3
Yyzhenzhen2010-04-11 10:35
//获取文件目录
           string fileName = @"filename";

            //开始转换
            //生成操作对象
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Documents documents =word.Documents;
            Type wordType = word.GetType();
            Type docType = documents.GetType();

            //打开文件
            Microsoft.Office.Interop.Word.Document document = (Microsoft.Office.Interop.Word.Document)
                docType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, (object)documents, new Object[] { fileName, true, true });
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
#4
冰晶羽扇2012-12-17 19:40
请问楼主如何解决这个问题的,我现在很纠结,谢谢~~~~
#5
小明1232015-08-07 08:43
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD")) {
                p.Kill();
            }
1