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?