DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004

    Question Read Contents DOC File

    Dear all;

    Does any body know (sample code) how to read content of document (.DOC) in c#. I need to compare my input text (textbox) then show the file name.


  2. #2
    Join Date
    Mar 2005
    Los Angeles, Calif. AKA: Gangsta Yoda™
    I wrote up a quick example of loading a word document into a rtf textbox control for you.

    Add a reference to MS Word xx.0 Object Library
    Add a openFileDialog control to your form
    Add two butons - btnClose and btnBrowse

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    namespace WordAutomation
    	public partial class Form1 : Form
    		public Form1()
    		private void btnClose_Click(object sender, EventArgs e)
    		private void btnBrowse_Click(object sender, EventArgs e)
    			openFileDialog1.CheckFileExists = true;
    			openFileDialog1.CheckPathExists = true;
    			openFileDialog1.Filter = "Word Document Files Only (*.doc)|*.doc";
    			openFileDialog1.FilterIndex = 0;
    			openFileDialog1.Multiselect = false;
    			openFileDialog1.ReadOnlyChecked = false;
    			openFileDialog1.ShowReadOnly = false;
    			openFileDialog1.Title = "Select Word Document to 'Read'.";
    			DialogResult oResult = openFileDialog1.ShowDialog();
    			if (oResult == DialogResult.OK)
    				this.txtFilePath.Text = openFileDialog1.FileName;
    				this.txtFilePath.Text = String.Empty;
    		private void btnOpen_Click(object sender, EventArgs e)
    			object fileName = @txtFilePath.Text;
    			object oNull = System.Reflection.Missing.Value;
    			object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    			Word.Application  oApp = new Microsoft.Office.Interop.Word.Application();
    			Word.Document oDoc = oApp.Documents.Open(ref fileName, ref oNull, ref oNull, 
    				ref oNull, ref oNull, ref oNull, ref oNull, ref oNull, ref oNull, 
    				ref oNull, ref oNull, ref oNull, ref oNull, ref oNull, ref oNull, ref oNull);
    			oApp.Visible = false;
    			this.richTextBox1.Text = oDoc.Content.Text;
    			oDoc.Close(ref doNotSaveChanges, ref oNull, ref oNull);
    			oDoc = null;
    			oApp.Quit(ref doNotSaveChanges, ref oNull, ref oNull);
    			oApp = null;
    Attached Images Attached Images
    Last edited by RobDog888; 07-14-2007 at 04:00 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer code questions via PMs.
    Microsoft MVP 2006-2009
    Office Development FAQ (VBA, VB6, VB.NET, C#)
    Software Engineer MCP (VB6 & .NET)
    Reps & Rating PostsVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address List

  3. #3
    Join Date
    Sep 2004

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Replies: 6
    Last Post: 08-16-2006, 10:32 PM
  3. Read existing INI file
    By Feldhege in forum .NET
    Replies: 3
    Last Post: 06-16-2006, 09:03 AM
  4. Replies: 1
    Last Post: 06-08-2006, 02:28 PM
  5. opening word doc, excel file etc
    By Sanjeev in forum ASP.NET
    Replies: 1
    Last Post: 11-16-2000, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.