如何关闭Word文档的运行实例? (C#) [英] how to close a running instance of Word document? (C#)

查看:478
本文介绍了如何关闭Word文档的运行实例? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了很多非常相似的线程各地,但没有什么似乎给我一个解决方案,它应该是非常基本的。

从我的WinForms应用程序,我需要关闭Word文档(应用程序本身打开)的运行实例。当我打开应用程序Word文档中,我一直在一个列表中它的轨道。现在,我怎么可以关闭相同的文档?

下面是我的尝试:

 私人布尔CloseWord(字符串osPath)//这里我传递文件的完全限定路径
{
    尝试
    {
        Word.Application应用=(Word.Application)Marshal.GetActiveObject(Word.Application);
        如果(应用程序== NULL)
            返回true;        的foreach(在app.Documents Word.Document D)
        {
            如果(d.FullName.ToLower()== osPath.ToLower())
            {
               d.What? //如何关闭在这里?
               返回true;
            }
        }
        返回true;
    }
    抓住
    {
        返回true;
    }
}

我得到了很多对文档对象的方法,但只有 .Close()收其中有参数是这样的: REF对象调用SaveChanges,参考对象OriginalFormat,参考对象RouteDocument 我不明白。

什么是理想的方式是什么?谢谢..

编辑:


  1. 我不能关闭整个Word应用程序(WINWORD),因为用户可能有其他的Word文件打开。


  2. 我只是需要终止字实例(如 Process.Kill()的东西),没有任何提示用户节省与否等。



解决方案

该解决方案从<一个得到href=\"http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/a2543c6d-8fc4-4b2f-bd19-fef3748cff18/\">here解决了。

  Word.Application应用=(Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(Word.Application);
如果(应用程序== NULL)
    返回true;的foreach(在app.Documents Word.Document D)
{
    如果(d.FullName.ToLower()== osPath.ToLower())
    {
        反对saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
        反对originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
        反对routeDocument = FALSE;
        d.Close(REF saveOption,裁判originalFormat,裁判routeDocument);
        返回true;
    }
}
返回true;

I could see a lot of very similar threads all around, but nothing seem to give me a solution which ought to be very basic.

From my winforms application, I need to close a running instance of a word document (opened from the application itself). When I open the word document from the application, I keep a track of it in a list. Now how can I close the same doc?

Here is what I tried:

private bool CloseWord(string osPath) //here I pass the fully qualified path of the file
{
    try
    {
        Word.Application app = (Word.Application)Marshal.GetActiveObject("Word.Application");
        if (app == null)
            return true;

        foreach (Word.Document d in app.Documents)
        {
            if (d.FullName.ToLower() == osPath.ToLower())
            {
               d.What? //How to close here?
               return true;
            }
        }
        return true;
    }
    catch
    {
        return true;
    }
}

I get a lot of methods for the document object, but only a .Close() to close which has arguments like this: ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument which I dont understand.

What is the ideal way? Thanks..

Edit:

  1. I can not close the entire Word application (WinWord) as users might have other word files opened.

  2. I need to just terminate the word instance (something like Process.Kill()) without any prompt for user to save or not etc.

解决方案

This solution got from here solves.

Word.Application app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if (app == null)
    return true;

foreach (Word.Document d in app.Documents)
{
    if (d.FullName.ToLower() == osPath.ToLower())
    {
        object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
        object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
        object routeDocument = false;
        d.Close(ref saveOption, ref originalFormat, ref routeDocument);
        return true;
    }
}
return true;

这篇关于如何关闭Word文档的运行实例? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆