Microsoft.Office.Interop.Word.dll wordApp.Documents.Open()在IIS中的asp.net网站中没有响应

时间:2017-10-29 13:05:55

标签: asp.net .net iis ms-word

我想使用Microsoft.Office.Interop.Word 15.0.4551.1009包搜索并替换asp.net中docx文件中的一些文本。 .Net Framework 4.5。 Office 2013

当我点击按钮时,以下是我的代码。

public void btnsave_Click(object sender, EventArgs e)
{       
           try
           {
              string CopyDoc = HostingEnvironment.ApplicationPhysicalPath + "CopyDoc" + "\\File1.docx";
              object fileName = CopyDoc ;       
              Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
              Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: false);
              aDoc.Activate();
              FindAndReplace(wordApp, "##NAME##", "ABCD");  //Method for Replace Text
              aDoc.Save();
              wordApp.Quit();
           }
           catch(Exception ex){ 
                  WriteActivityLog(Ex.Message+":::::"+Ex.StackTrace);
           }
}

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
{
    //options
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    //execute find and replace
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}

但是在执行时 wordApp.Documents.Open()行有一些麻烦... 浏览器符号似乎总是加载...意味着它没有响应。 我也写了捕获的日志,但它没有响应,没有给予和错误。

但是这个应用程序在我的本地系统的IIS中完美运行,并且还在Visual Studio 2013中运行。

我已经在服务器上安装了Microsoft Office。 我已在“C:\ Windows \ SysWOW64 \ config \ systemprofile”中创建桌面文件夹。 我已经更改了IIS应用程序池Identity = LocalSystem。 我已经更改 DCOM配置 Microsoft Word 97 - 2003文档属性标识选择交互式用户。

请帮助我,我能做什么?

1 个答案:

答案 0 :(得分:0)

如果您仍在寻找解决方案,请尝试以下

    static Word.Application wordApp = null;
    static Object oMissing = System.Reflection.Missing.Value;
    static Object oTrue = true;
    static Object oFalse = false;
    static Object oCopies = 1;

    private static void InitializeInstance()
    {
        if (wordApp == null)
        {
            wordApp = new Word.ApplicationClass();
            wordApp.Visible = false;
            wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;         
        }           
    }

    private static Word.Document OpenDocument(string documentPath)
    {
        InitializeInstance();

        Object objDocumentPath = documentPath;
        Word.Document wordDoc = wordApp.Documents.Open(ref objDocumentPath, ref oMissing, ref oMissing, ref oMissing,ref oMissing,
                    ref oMissing, ref oMissing,ref oMissing,ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        return wordDoc;
    }

    private static void CloseDocument(Word.Document wordDoc)
    {
        if (wordDoc != null)
        {
            wordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
            wordDoc = null;
        }           
    }

    public static void CloseInstance()
    {
        if (wordApp != null)
        {
            wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
            wordApp = null;
        }
    }

    public static void KillInstances()
    {
        try
        {
            Process[] processes = Process.GetProcessesByName("WINWORD");
            foreach(Process process in processes)
            {
                process.Kill();
            }
        }
        catch(Exception)
        {

        }
    }