2017-10-29 8 views
1

Microsoft.Office.Interopを使用してasp.netのdocxファイルのテキストを検索して置き換えたいとします。ワード15.0.4551.1009パッケージ。 .Net Framework 4.5。 Office 2013Microsoft.Office.Interop.Word.dll wordApp.Documents.Open()がIISのasp.net Webサイトで応答しない

次のコードは、ボタンをクリックしたときです。

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での完璧な実行されていて、また、私はすでにサーバー上のMicrosoft OfficeをインストールされているVisual Studioの2013年

を実行しています。 私はすでに "C:\ Windows \ SysWOW64 \ config \ systemprofile"にデスクトップフォルダを作成しています。 私はすでにIISアプリケーションプールID = LocalSystemを変更しています。 私はすでに変更済みDCOM構成Microsoft Word 97 - 2003文書プロパティID選択対話ユーザー。

どうすればいいですか?

+0

可能な重複してみてくださいその後、解決策を探している場合は、[OfficeのInteropsを実行するために、サーバー側は何が必要ですか?](https://stackoverflow.com/questions/26224066 /必要なサーバー側から実行中のオフィス間) –

+0

サーバー側のOfficeオートメーションは禁止されています(https://support.microsoft.com/en-us/help/257757/considerations-for-server)。 -side-automation-of-office –

+0

@LexLiお返事ありがとうございますが、私はすでにMicrosoft Officeをインストールしています。私はまた、すべてのソリューションを試してみますが、うまくいきませんでした。 –

答えて

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

     } 
    } 
関連する問題