Search code examples
c#windowswinformsdoc

read .doc file in c# windows form


i am trying to read .doc and .docx file in c# but it is not displaying as we see in document file. it just read text i want solution where i can see tables, design, images we can see in document file please help

Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object fileName = @"C:\wordFile.docx";
// Define an object to pass to the API for missing parameters
object missing = System.Type.Missing;                
doc = word.Documents.Open(ref fileName,
                          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, ref missing, ref missing);
string ReadValue = string.Empty;
// Activate the document
doc.Activate();

foreach (Word.Range tmpRange in doc.StoryRanges)
{
    ReadValue += tmpRange.Text;
}

in above solution we can read only text in document file.


Solution

  • Unfortunately there is no trivial implementation for this, since Word has a lot of features and formatting options.

    If you have the money, you can purchase 3rd party controls for that (just do a search for "Winforms Word control").

    If you have the time (and you're interested in the details), there is an old control with the complete sourcecode on CodeProject: Word control for .NET