Read the Content of a Word File into A String (From Server)

This code will read the file contents of Word File into string Varriable


//Mark Reference to Microsoft Word 12.0 Object Library
//Project-RightClick-AddReference-ComTab-Microsoft Word 12.0 Object Library-Ok
//Create instance for the Word Object
Microsoft.Office.Interop.Word.Application ObjWord = new Microsoft.Office.Interop.Word.Application();
//Create default values
object falseValue = false;
object trueValue = true;
object missing = Type.Missing;
//set the options
object saveobjections = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
//Create instance for Word Document
Microsoft.Office.Interop.Word.Document wrdoc;
//Get the Filepath
//Your files should have in the Application path and insides files folder
object filepath = Server.MapPath(".") + @"\Files\" + @"MVP Nomination Form.doc";
//Open the File
wrdoc = ObjWord.Documents.Open(ref filepath, ref missing, ref trueValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//Read the FileContents in A string Varriable
string sFileContens = wrdoc.Content.Text; //get the Text

//Close the Current document
ObjWord.Documents.Close(ref saveobjections, ref missing, ref missing);
//Quit word
ObjWord.Quit(ref saveobjections, ref missing, ref missing);


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: