本文介绍了字自动化 - 文件正在使用由其他应用程序或用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序,其中我使用Word自动化建设通过模板的文档,然后将其保存到数据库中。在创建文档后,我从数据库中检索文件,把它写在一个临时目录中的文件系统,然后使用Word互操作的服务打开文档。

I have a WinForms application where I am using Word Automation to build documents via a template, and then save them to the database. After the document is created, I retrieve the document from the database, write it to the file system in a temp directory, and then open the document using the Word Interop services.

有装载文档列表以及用户可以打开每个文档的仅1个实例,但可同时打开多个不同的文档。我没有与打开,保存和关闭任何问题,当他们打开1号文件,然而,当他们同时打开多个文档,我关闭所有的Word我的情况下,当出现以下错误:

There is a list of documents loaded and the user can open only 1 instance of each document, but can open multiple different documents simultaneously. I don't have any problems with opening, saving, and closing when they open 1 document, however, when they open multiple documents simultaneously, I get the following error when closing any of my instances of Word:

The file is in use by another application or user. (C:\...\Templates\Normal.dotm)
This error is commonly encountered when a read lock is set on the file that you are attempting to open.

我使用下面的code打开文档和处理BeforeDocumentClosed事件:

I am using the following code to open the document and handle the BeforeDocumentClosed event:

public void OpenDocument(string filePath, Protocol protocol, string docTitle, byte[] document)
{
    _protocol = protocol;
    documentTitle = docTitle;
    _path = filePath;

    if (!_wordDocuments.ContainsKey(_path))
    {
        FileUtility.WriteToFileSystem(filePath, document);

        Word.Application wordApplication = new Word.Application();
        wordApplication.DocumentBeforeClose += WordApplicationDocumentBeforeClose;

        wordApplication.Documents.Open(_path);

        _wordDocuments.Add(_path, wordApplication);
    }
    _wordApplication = _wordDocuments[_path];
    _currentWordDocument = _wordApplication.ActiveDocument;

    ShowWordApplication();
}

public void ShowWordApplication()
{
    if (_wordApplication != null)
    {
        _wordApplication.Visible = true;
        _wordApplication.Activate();
        _wordApplication.ActiveWindow.SetFocus();
    }
}

private void WordApplicationDocumentBeforeClose(Document doc, ref bool cancel)
{
    if (!_currentWordDocument.Saved)
    {
        DialogResult dr = MessageHandler.ShowConfirmationYnc(String.Format(Strings.DocumentNotSavedMsg, _documentTitle), Strings.DocumentNotSavedCaption);

        switch (dr)
        {
            case DialogResult.Yes:
                SaveDocument(_path);
                break;
            case DialogResult.Cancel:
                cancel = true;
                return;
        }
    }

    try
    {
        if (_currentWordDocument != null)
            _currentWordDocument.Close();
    }
    finally
    {
        Cleanup();
    }
}

public void Cleanup()
{
    if (_currentWordDocument != null)
        while(Marshal.ReleaseComObject(_currentWordDocument) > 0);

    if (_wordApplication != null)
    {
        _wordApplication.Quit();
        while (Marshal.ReleaseComObject(_wordApplication) > 0);
        _wordDocuments.Remove(_path);
    }
}

有谁看到什么问题,我做的,允许在同一时间开启多个文件?我是相当新的Word中的自动化和话语互操作服务,所以任何的建议是AP preciated。谢谢你。

Does anyone see anything wrong that I am doing to allow opening of multiple documents at the same time? I am fairly new to Word Automation and the Word Interop services, so any advice is appreciated. Thanks.

推荐答案

我发现通过这个MSDN文章的解决方案:的http:// support.microsoft.com/kb/285885

I found the solution via this MSDN article: http://support.microsoft.com/kb/285885

您需要调用Application.Quit()之前,要做到这一点;

You need to do this before calling Application.Quit();

_wordApplication.NormalTemplate.Saved = true;

这prevents从试图拯救的Normal.dotm模板字。我希望这可以帮助别人。

This prevents Word from trying to save the Normal.dotm template. I hope this helps someone else.

这篇关于字自动化 - 文件正在使用由其他应用程序或用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:00
查看更多