views:

197

answers:

4

i want to read a microsoft word file without use of data connection,

+1  A: 

If by "doc" you mean "Word 2003 document" then it's not a plain text file to start with - it's a binary file format. I'm not sure whether it's documented anywhere, although clearly projects like Open Office have reverse-engineered it.

If by "doc" you mean something else, please clarify.

Jon Skeet
+1  A: 

Word documents use the .doc extension, and can optionally be saved in an XML-based format. If you have the option of doing that instead, you can use an XML parsing library to get at the content. The whole schema is pretty complicated but you may be able to extract some useful things from it in a simple way.

Daniel Earwicker
+2  A: 

".doc" is a not a simple text based file format. You have to use interop for maniuplation.

Include the COM Libary "Microsoft Word 12.0 Object Library". Create an ApplicationClass and use the property Documents to open your document.

        object wordPath = null;
        object missing = System.Reflection.Missing.Value;

        wordPath = @"C:\sample.doc";

        // Create Interop object
        ApplicationClass word = new ApplicationClass();
        word.Visible = false;

        // Open document
        Document doc = word.Documents.Open(ref wordPath,
            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);

        // Set document as active for interaction
        doc.Activate();

        // Select the whole content of the word document
        word.Selection.WholeStory();

        // Get the text from the document
        string text = word.Selection.Text;

There is a very good introduction at the blog of Scott C. Reynolds.

michl86
i want to read excel file from Listview to search email id from excel file.... i am trying to do this...but i dont know the type of encoding of excel i mean dataformates... how can i read a excel file to search email id...i dont want to use data connection
ankush
like for word, there is also an object libary for excel. the code should be very similar. with the worksheet object you can get a range. this range has a text property. but i think the use of data connection makes it much more easier...
michl86
A: 

object wordPath = null; object missing = System.Reflection.Missing.Value;

    wordPath = @"C:\sample.doc";

    // Create Interop object
    ApplicationClass word = new ApplicationClass();
    word.Visible = false;

    // Open document
    Document doc = word.Documents.Open(ref wordPath,
        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);

    // Set document as active for interaction
    doc.Activate();

    // Select the whole content of the word document
    word.Selection.WholeStory();

    // Get the text from the document
    string text = word.Selection.Text;
ankush