DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Reading word file (http://www.daniweb.com/forums/thread168237.html)

gallian99 Jan 14th, 2009 12:44 am
Reading word file
 
How can I read the contents (separate paragraphs) of a .doc /.docx file in C#

Ramy Mahrous Jan 14th, 2009 1:26 am
Re: Reading word file
 
Very easy...
Project->Add Reference->COM
tab and look for the Microsoft Word X.x Object Library
Use ApplicationClass class. Enjoy...

gallian99 Jan 14th, 2009 5:24 am
Re: Reading word file
 
I have added the reference, but could you be more elaborative with the ApplicaitonClass usage?

Ramy Mahrous Jan 14th, 2009 8:08 am
Re: Reading word file
 
No, I need you to try using it, I could tell you some code and your problem been solved! but I need you to try to learn. Please try

gallian99 Jan 15th, 2009 4:20 pm
Re: Reading word file
 
Is there a way I could run it in background. i.e. without opening the word file.

Have manage to read the contents but it one can certainly notice the opening and closing of the world doc

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

object fileNameO = fileName;//"c:\\sample.doc";
object objFalse = false;
object objTrue = true;
object missing = System.Reflection.Missing.Value;
object emptyData = string.Empty;

try
{
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileNameO, ref objFalse, ref objTrue,
ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
ref missing, ref missing, ref missing, ref missing);

aDoc.ActiveWindow.Selection.WholeStory();
aDoc.ActiveWindow.Selection.Copy();


IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
String filetext = data.GetData(System.Windows.Forms.DataFormats.Text).ToString();
System.Windows.Forms.Clipboard.SetDataObject(string.Empty);
richTextBox1.Text = filetext;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
wordApp.Documents.Close(ref missing, ref missing, ref missing);
wordApp.Application.Quit(ref missing, ref missing, ref missing);
}
}

Quote:

Originally Posted by RamyMahrous (Post 778064)
No, I need you to try using it, I could tell you some code and your problem been solved! but I need you to try to learn. Please try


Ramy Mahrous Jan 15th, 2009 6:59 pm
Re: Reading word file
 
Look .doc file is binary file I think you can't do that without word, but .docx is follow OOXML you can play with it.


All times are GMT -4. The time now is 4:41 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC