【问题标题】:Microsoft.Office.Interop.Word.dll wordApp.Documents.Open() not responding in asp.net website in IISMicrosoft.Office.Interop.Word.dll wordApp.Documents.Open() 在 IIS 的 asp.net 网站中没有响应
【发布时间】:2017-10-29 13:05:54
【问题描述】:

我想使用 Microsoft.Office.Interop.Word 15.0.4551.1009 包在 asp.net 的 docx 文件中搜索和替换一些文本。 .Net 框架 4.5。办公室 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() 行有一些问题... 浏览器符号似乎总是加载......意味着它没有响应。 我还在 catch 中写了日志,但它没有响应,也没有给出和错误。

但是这个应用程序非常适合在我的本地系统的 IIS 中运行,也可以在 Visual Studio 2013 中运行。

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

请帮帮我,我能做什么?

【问题讨论】:

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


【解决方案1】:

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

    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)
        {

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2011-10-06
    • 2015-09-02
    • 1970-01-01
    • 2018-09-23
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多