Read Contents DOC File


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Read Contents DOC File

  1. #1
    Join Date
    Sep 2004
    Posts
    56

    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.

    Thank's
    GOEN

  2. #2
    Join Date
    Mar 2005
    Location
    Los Angeles, Calif. AKA: Gangsta Yoda™
    Posts
    455
    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



    Code:
    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()
    		{
    			InitializeComponent();
    		}
    
    		private void btnClose_Click(object sender, EventArgs e)
    		{
    			Application.Exit();
    		}
    
    		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;
    			}
    			else
    			{
    				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
    Posts
    56
    thank's

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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