Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to highlight all occurrences of a word in a Word document??

How to highlight all occurrences of a word in a Word document??

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fiaolle
    wrote on last edited by
    #1

    Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.

    Word.Application ap = new Word.Application();
    doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
    ap.Selection.Find.ClearFormatting();
    ap.Selection.Find.Text = txtFind.Text;
    ap.Selection.Find.Execute();
    while (ap.Selection.Find.Found)
    {
    ap.Selection.Select();
    ap.Selection.Find.Execute();
    }
    doc.Activate();
    ap.Visible = true;
    ap.ActiveWindow.SetFocus();

    I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia

    P J 2 Replies Last reply
    0
    • F fiaolle

      Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.

      Word.Application ap = new Word.Application();
      doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
      ap.Selection.Find.ClearFormatting();
      ap.Selection.Find.Text = txtFind.Text;
      ap.Selection.Find.Execute();
      while (ap.Selection.Find.Found)
      {
      ap.Selection.Select();
      ap.Selection.Find.Execute();
      }
      doc.Activate();
      ap.Visible = true;
      ap.ActiveWindow.SetFocus();

      I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You are missing some properties for your Selection.Find list. Try setting Forward = true and Wrap = wdFindContinue. To find these values, you can use the macro recorder in Word and perform a Find all operation - this should give you a good place to start.

      Forgive your enemies - it messes with their heads

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      F 1 Reply Last reply
      0
      • P Pete OHanlon

        You are missing some properties for your Selection.Find list. Try setting Forward = true and Wrap = wdFindContinue. To find these values, you can use the macro recorder in Word and perform a Find all operation - this should give you a good place to start.

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        F Offline
        F Offline
        fiaolle
        wrote on last edited by
        #3

        Hi Thanks for your reply, but I have already tried that, but nothing changes. It just selects the last occurrence of the word. I really don't want to use a macro. Aren't there another way to accomplish this. Many thanks Fia

        P L 2 Replies Last reply
        0
        • F fiaolle

          Hi Thanks for your reply, but I have already tried that, but nothing changes. It just selects the last occurrence of the word. I really don't want to use a macro. Aren't there another way to accomplish this. Many thanks Fia

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Use the macro to tell you what options to set, not to actually run the find.

          Forgive your enemies - it messes with their heads

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          F 1 Reply Last reply
          0
          • P Pete OHanlon

            Use the macro to tell you what options to set, not to actually run the find.

            Forgive your enemies - it messes with their heads

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            F Offline
            F Offline
            fiaolle
            wrote on last edited by
            #5

            Hi Thank you, that was smart. But when I record a macro that does what I want and later tries to run the macro nothing happens. I don't understand why? Do you have anymore suggestions. Many thanks Fia

            1 Reply Last reply
            0
            • F fiaolle

              Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.

              Word.Application ap = new Word.Application();
              doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
              ap.Selection.Find.ClearFormatting();
              ap.Selection.Find.Text = txtFind.Text;
              ap.Selection.Find.Execute();
              while (ap.Selection.Find.Found)
              {
              ap.Selection.Select();
              ap.Selection.Find.Execute();
              }
              doc.Activate();
              ap.Visible = true;
              ap.ActiveWindow.SetFocus();

              I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Repeating what was already said.... 1. Create a macro that does what you want. 2. LOOK at the macro. Figure out what it is doing. 3. Use the information that you LEARNed in 2 to create your C# code.

              F 1 Reply Last reply
              0
              • J jschell

                Repeating what was already said.... 1. Create a macro that does what you want. 2. LOOK at the macro. Figure out what it is doing. 3. Use the information that you LEARNed in 2 to create your C# code.

                F Offline
                F Offline
                fiaolle
                wrote on last edited by
                #7

                Hi I have already tried that but it doesn't work. I have no idea how to solve this. If anyone knows please help. Thanks Fia

                1 Reply Last reply
                0
                • F fiaolle

                  Hi Thanks for your reply, but I have already tried that, but nothing changes. It just selects the last occurrence of the word. I really don't want to use a macro. Aren't there another way to accomplish this. Many thanks Fia

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  fiaolle wrote:

                  It just selects the last occurrence of the word.

                  May be you have to set MultiSelect = true somewhere in your code. Just a suggestion though, I haven't done Word automation so far.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups