Tuesday, November 12, 2013

Microsoft.Office.Interop.Word how to prevent word file opening

In my previous post i explained, how to Convert the dataset to xml string using  C# and asp.net
Now in this article i will explain how to prevent the word file opening, i have bunch of word document and i want to read/search text value out of it.  i tried that using Microsoft.office.Interop.Word object. 

Here is the sample code which prevent the word to opening the main window.
private void PreventOpenWord(string Filepath)
{
 Application word = new Application();
 FileInfo fileInfo = (FileInfo)file;


object save = false;
object confirmConversion = false;
object visible = false;
object skipEncodingDialog = true;
object readOnly = true;
object filename = fileInfo.FullName;

word.Visible = false;

Document srcDoc = word.Documents.Open(ref Filepath, ref confirmConversion, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref visible,
    ref missing, ref missing, ref skipEncodingDialog, ref missing);




foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
{
if (docRange.Text.Trim().Equals(textToFind,
StringComparison.CurrentCultureIgnoreCase))
{
  
IsWordFound = true;
break;
}
}
}
catch (Exception ex)
{
 Response.Write("Error while reading file : " + ex.Message);
}
}


Hope this will helps you.. Happy  coding.. 

No comments:

Post a Comment