none
i want to search a string(word) from .doc files RRS feed

  • Question

  • i want to search for a word(sting) in my directory in that  i have saved .doc files.

    if i enter a word(ex:- dotnet) it has to search all .doc files where i have created a folder and saved .doc files  if that word finds it has to show the file names as list

    i want it in c# pls


    • Moved by Helen Zhou Tuesday, June 7, 2011 9:27 AM (From:Windows Forms General)
    Monday, June 6, 2011 5:52 AM

Answers

  • Hi,

    Here is the workaround:

    1. Add reference to Word Object

    2. Add a button name "button1", a textbox named "textbox1", add a listbox name "listbox1"

    3. Code the application like below:

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace FindWord

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            object missing = System.Type.Missing;

            object notTrue = false;

            private void button1_Click(object sender, EventArgs e)

            {

                button1.Enabled = false;

                listBox1.Items.Clear();         

                string path = @"D:\Test";

                string[] files = Directory.GetFiles(path);

                foreach (string filePath in files)

                {

                    if (isWordDocument (filePath )== true)

                    {

                        if (HaveFind(filePath) == true)

                         {

                            listBox1.Items.Add(filePath);

                        }

                    }

                }

                button1.Enabled = true;

            }

            public bool isWordDocument(string filePath)

            {

                bool isWord = false;

                if (Path.GetExtension(filePath) == ".docx")

                {

                    isWord = true;

                }

                return isWord;

            }

     

            public bool HaveFind(string filePath)

            {

                Word.Application app = new Word.Application();

                Word.Document doc = null;     

                bool Ifind = false;

                try

                {

                    doc = app.Documents.Open(filePath);

                    doc.Activate();

                    foreach (Word.Range range in doc.StoryRanges)

                    {

                        range.Find.Text = textBox1.Text;

                        range.Find.Wrap = Word.WdFindWrap.wdFindStop;

                        Ifind = range.Find.Execute(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);

                    }

                    ((Word._Document)doc).Close();

                    doc = null;

                }

                catch (Exception ex) { }

     

                ((Word._Application)app).Quit();

                app = null;

                return Ifind;   

            }

        }

    }


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 11:21 AM
    Moderator

All replies

  • http://www.codeproject.com/Answers/70436/How-can-I-search-content-of-document-doc-in-Csharp.aspx#answer1

    Kindly have a look at the above link which can be useful.

    Regards,

    Narendran Ponpandiyan

    Monday, June 6, 2011 8:58 AM
  • Hi NrK,

    This is more related to VSTO, I'm moving it to Word for developers forum to help you get better response.

    Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 7, 2011 9:27 AM
  • The following code is from one of my applications. It will show you how you can access a directory of files and then do something with those files. Since I don't write in C, someone else will have to translate.

      Private Sub Transfer_Masters()
        Dim s, fileName, destFile As String
        Dim tArray As Array = Nothing
    
        Try
          If My.Computer.FileSystem.DirectoryExists(pfMasters) Then
            My.Computer.FileSystem.CopyDirectory(pfMasters, LGPMasters, True)
          End If
          tArray = System.IO.Directory.GetFiles(pfTemplates)
          If System.IO.Directory.Exists(pfTemplates) Then
            For Each s In tArray
              fileName = System.IO.Path.GetFileName(s)
              destFile = System.IO.Path.Combine(LGPTemplate, fileName)
              System.IO.File.Copy(s, destFile, True)
            Next
          End If
    
          Call Transfer_Library()
    
        Catch ex As Exception
          MsgBox("Error Transferring Masters", MsgBoxStyle.Critical, GCL)
    
        End Try
    
      End Sub
    
    

    The variable pfMasters, pfTemplates, LGPMasters, and LGPTemplate are strings that contain directory paths.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, June 7, 2011 10:44 AM
  • Hi Nrk, 

    The code provided by Rich lead you a reasonable approach to iterate over files under a directory.

    For additionally information, here are some information about how to find text in word documents:

    Find.Execute Method 

    How to: Search for Text in Documents 

    If you have any further concern, please feel free to let me know.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 8, 2011 2:38 AM
    Moderator
  • hi Calvin_Gao if possible can u send a sample applications in c# (website) 

     

    Thanks in advace

     

    Ravikumar.N

    Monday, June 13, 2011 9:24 AM
  • Hi,

    Here is the workaround:

    1. Add reference to Word Object

    2. Add a button name "button1", a textbox named "textbox1", add a listbox name "listbox1"

    3. Code the application like below:

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace FindWord

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            object missing = System.Type.Missing;

            object notTrue = false;

            private void button1_Click(object sender, EventArgs e)

            {

                button1.Enabled = false;

                listBox1.Items.Clear();         

                string path = @"D:\Test";

                string[] files = Directory.GetFiles(path);

                foreach (string filePath in files)

                {

                    if (isWordDocument (filePath )== true)

                    {

                        if (HaveFind(filePath) == true)

                         {

                            listBox1.Items.Add(filePath);

                        }

                    }

                }

                button1.Enabled = true;

            }

            public bool isWordDocument(string filePath)

            {

                bool isWord = false;

                if (Path.GetExtension(filePath) == ".docx")

                {

                    isWord = true;

                }

                return isWord;

            }

     

            public bool HaveFind(string filePath)

            {

                Word.Application app = new Word.Application();

                Word.Document doc = null;     

                bool Ifind = false;

                try

                {

                    doc = app.Documents.Open(filePath);

                    doc.Activate();

                    foreach (Word.Range range in doc.StoryRanges)

                    {

                        range.Find.Text = textBox1.Text;

                        range.Find.Wrap = Word.WdFindWrap.wdFindStop;

                        Ifind = range.Find.Execute(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);

                    }

                    ((Word._Document)doc).Close();

                    doc = null;

                }

                catch (Exception ex) { }

     

                ((Word._Application)app).Quit();

                app = null;

                return Ifind;   

            }

        }

    }


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 11:21 AM
    Moderator