1

I want to search and replace some text in docx file in asp.net using Microsoft.Office.Interop.Word 15.0.4551.1009 package. .Net Framework 4.5. Office 2013

Following is my code when i clicked on the button.

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);
}

But it has some trouble on wordApp.Documents.Open() line when executing... Browser symbol seems like loading always... means its not responding. I also write log in the catch but its not responding and no giving and error.

But this application is perfect running in the my Local System's IIS and Also running in the Visual Studio 2013.

I already Installed Microsoft Office on the server. I already Create Desktop folder in "C:\Windows\SysWOW64\config\systemprofile". I already Change IIS Application Pools Identity = LocalSystem. I already Change DCOM Config Microsoft Word 97 - 2003 Document Properties Identity Select The interactive user.

Please Help Me, What i can do?

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Monark Patel
  • 159
  • 2
  • 15
  • Possible duplicate of [What is required server-side to run Office Interops?](https://stackoverflow.com/questions/26224066/what-is-required-server-side-to-run-office-interops) – Lex Li Oct 29 '17 at 13:36
  • Server side Office automation is forbidden, https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office – Lex Li Oct 29 '17 at 13:37
  • @LexLi Thanks for reply me, But I already install Microsoft Office. and i also try the All the solutions but it`s did not work. – Monark Patel Oct 29 '17 at 13:52
  • I hope you know some English in order to understand what Microsoft says in the Knowledge Base article. In the meantime, Microsoft site might contain localized translation copy if you prefer your native language. – Lex Li Oct 29 '17 at 13:54

1 Answers1

0

If you are still looking for a solution then try the following

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

        }
    }
Satya
  • 1
  • 1