1

大家好,我正在使用互操作在 MS-Word 中打开几个文件。它工作正常。问题是当我尝试打开已经打开的文件时不起作用。任务管理器中出现正在使用的文件对话框能够只能通过在任务管理器中单击它来访问它。我怎样才能使它可见?或者你能建议任何其他方法吗?

在此处输入图像描述

  Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
    WordApp.DisplayAlerts = WdAlertLevel.wdAlertsAll;
    Microsoft.Office.Interop.Word.Document WordDoc = new Microsoft.Office.Interop.Word.Document();
    WordDoc = WordApp.Documents.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing);
    WordApp.Visible = true;
    System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
4

3 回答 3

1

如果您需要仅使用只读功能第二次打开文件(例如,如果您将其用作模板),那么我看不出原因您不应该只复制它并每次都打开副本你需要它。

也许您可以添加其他信息,所以也许有人有更好的建议来找到解决方案!

于 2012-12-27T19:51:12.750 回答
0

Make sure to use this when unloading the interop:

WordObject.Quit

Releasing the interop object somehow does not allways work.

Check out additional info on .Quit http://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.word._application.quit(v=office.11).aspx

You can make interop application to become visible using

WordObject.Visible = true

You should also bare in mind, that You should NEVER use office interop objects on server-side processes for document processing, generation etc, because it just is not ment for doing so. Interop object are memory and cpu hungry, realy unstable and crash a lot!

于 2012-12-27T10:19:57.983 回答
0

您可以尝试将您的文件加载到另一个AppDomain中,您可以在不需要它后将其卸载

AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.doc");
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
bool ok = proxy.DoMsWork("C:\\some.doc");
AppDomain.Unload(ad2);

    public class ProxyDomain : MarshalByRefObject
    {
        public bool DoMsWork(string assemblyPath)
        {
            //Load your file and do work here
        }
    }
于 2012-12-27T09:38:46.233 回答