none
Find paragraph by using that text located in RRS feed

  • Question

  • I need to search paragraph by giving paragraph heading text associated with it and to which some heading are set like wdOutlineLevel1, wdOutlineLevel2. 

    For example:

    Feature A

    paragraph text- A chip is a major building block of an electronic system such as an ASIC or an FPGA.

    I want to find the above paragraph by giving only "Feature A" without iterating on all paragraphs of the document.

    I have set "heading 2" for text "Feature A" and "paragraph text-......" is the normal text.

    Any help will be appreciable.

    Friday, May 9, 2014 1:31 PM

Answers

  • Here's some VBA code you can use to Find all the text associated with your 'Feature A' heading 2. It returns whatever occurs between that and the next Heading 1, Heading 2 or the document end, whichever comes first. I'll let someone else sort out the C# conversion.

    Sub GetData()
    Application.ScreenUpdating = False
    Dim wdDoc As Document, wdRng As Range
    Set wdDoc = ActiveDocument
    With wdDoc
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Style = "Heading 2"
          .Execute "Feature A^p", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
        End With
        If .Find.Found Then
          Set wdRng = .Duplicate
          wdRng.Collapse 0 'wdCollapseEnd
          .Start = wdRng.End
          With .Find
            .Style = "Heading 1"
            .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
          End With
          If .Find.Found Then
            wdRng.End = .Duplicate.Start - 1
            With wdRng.Duplicate
              With .Find
                .Style = "Heading 2"
                .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
              End With
              If .Find.Found Then
                wdRng.End = .Start - 1
              End If
            End With
          Else
            With .Find
              .Style = "Heading 2"
              .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
            End With
            If .Find.Found Then
              wdRng.End = .Duplicate.Start - 1
            Else
              wdRng.End = wdDoc.Range.End
            End If
          End If
          If Not wdRng Is Nothing Then
            MsgBox wdRng.Text
          End If
        End If
      End With
    End With
    Set wdRng = Nothing: Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 9:00 AM
  • Hi Saurahb,

    >>This code works when same paragraph text present in the document but it is not necessary that same paragraph text should be present. I need All paragraphs after 'Feature A' which have headings below the 'Feature A' paragraph heading.<<

    If you want to get the paragraphs after the specifc text you found, you colud use Range.Next to find the next paragraph after the specific text.

    Here is a sample for your reference:

     public void Main()
            {
                object missing = Type.Missing;
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                Word.Document doc = wordApp.Documents.Open(@"C:\Users\v-fexue\Desktop\TestFind.docx");
                Word.Range rng = doc.Content;
                int startPostion = 0;
                object findText = "Feature A";
                object textFormat = "Heading 2";
    
                rng.Find.ClearFormatting();
                rng.Find.Text = "Feature A";
                rng.Find.set_Style(ref textFormat);
                if (rng.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))
                    startPostion = rng.End;
    
    
                if (rng.Find.Found)
                {
                    Range targetRng=rng.Next(WdUnits.wdParagraph, 1);
                    targetRng.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
                }
    
            }

    If I misunderstood, please feel free to let me know.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 12, 2014 10:03 AM
    Moderator
  • HI Saurabh

    There's a predefined bookmark in Word that can get the current Heading level plus all paragraphs up to the next occurrence of the same heading level - that's what I understand you to want. In the VBA venacular:
      Doc.Bookmarks("\HeadingLevel").Range

    In C# I believe you need

     object bmName = @"\HeadingLevel";
     Doc.Bookmarks.get_Item(ref bmName).Range


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, May 12, 2014 2:44 PM
    Moderator
  • That's because you have to use the code exactly as a gave it. \paragraph1 is not a built-in Word bookmark.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 13, 2014 2:27 PM
    Moderator

All replies

  • I need to search paragraph by giving paragraph heading text associated with it and to which some heading are set like wdOutlineLevel1, wdOutlineLevel2. 

    For example:

    Feature A

    paragraph text- A chip is a major building block of an electronic system such as an ASIC or an FPGA.

    I want to find the above paragraph by giving only "Feature A" without iterating on all paragraphs of the document.

    I have set "heading 2" for text "Feature A" and "paragraph text-......" is the normal text.

    Any help will be appreciable.


    Friday, May 9, 2014 1:01 PM
  • Hello Saurabh,

    The current forum is for VSTO related questions. I'd suggest asking Word specific questions in the Word for Developers forum instead. There you will get the most qualified feedback.

    Friday, May 9, 2014 1:15 PM
  • Assuming the text you want is in the next paragraph after 'Feature A', you can use Find for this. For example:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Feature A"
        .Style = "Heading 2"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      If .Find.Found Then
        MsgBox Chr(34) & .Text & Chr(34) & " is in:" & vbCr & _
        .Paragraphs.Last.Range.Text & vbCr & vbCr & _
        "The next paragraph is:" & vbCr & _
        .Paragraphs.Last.Next.Range.Text
      End If
    End With
    Application.ScreenUpdating = True
    End Sub

    Note: If your documents have empty paragraphs between the headings and the text that follows for spacing (bad practice) you will need additional .Next expressions (e.g. .Paragraphs.Last.Next.Next.Range.Text).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, May 9, 2014 10:23 PM
  • Thanks macropod.

    I need paragraph 'Feature A' and all paragraphs which comes inside 'Feature A'(All inner paragraphs of 'Feature A' paragraph).

    Actually I'm using C# so please explain code in C#.

    Regards,

    Saurabh 

    Saturday, May 10, 2014 6:51 AM
  • I can do that with VBA but I don't know C# code.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, May 10, 2014 12:13 PM
  • Hi Saurahb,

    Based on the description, you want to find the paragraphs in the specific text. You can user Rang.Find to find the specific text and then use Range.Start and Range.End to get the position of the text. At last, you can get the paragraphs via Range(startPostion, endPostion). Paragraphs.

    Here is a sample for your reference:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using Word = Microsoft.Office.Interop.Word;
    
    
    namespace ConsoleApplication8
    
    {
    
        class FindPara
    
        {
    
            public void Main()
    
            {
    
                object missing = Type.Missing;
    
                Word.Application wordApp = new Word.Application();
    
                wordApp.Visible = true;
    
                Word.Document doc = wordApp.Documents.Open(@"C:\Users\v-fexue\Desktop\TestFind.docx");
    
                Word.Range rng = doc.Content;
    
                int startPostion = 0, endPostion = 0;
    
                object findText = "Feature A";
    
                object textFormat = "Heading 2";
    
    
                rng.Find.ClearFormatting();
    
                rng.Find.Text = "Feature A";
    
                rng.Find.set_Style(ref textFormat);
    
                if (rng.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))
    
                    startPostion = rng.End;
    
    
                if (rng.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))
    
                    endPostion = rng.Start;
    
                if (startPostion != 0 && endPostion != 0)
    
                {
    
                    Word.Range targetRng = doc.Range(startPostion, endPostion);
    
                    Console.WriteLine("There are {0} paragraphs", targetRng.Paragraphs.Count);
    
                    doc.Range(startPostion, endPostion).HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
    
                }
    
               
    
            }
    
        }
    
    }
    

    Screen Shot:


    You can get more detail about searching text in document from link below:

    How to: Programmatically Search for and Replace Text in Documents

    How to: Programmatically Loop Through Found Items in Documents

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 12, 2014 6:23 AM
    Moderator
  • Thanks Fei Xue.

    This code works when same paragraph text present in the document but it is not necessary that same paragraph text should be present. I need All paragraphs after 'Feature A' which have headings below the 'Feature A' paragraph heading.


    Monday, May 12, 2014 8:09 AM
  • Here's some VBA code you can use to Find all the text associated with your 'Feature A' heading 2. It returns whatever occurs between that and the next Heading 1, Heading 2 or the document end, whichever comes first. I'll let someone else sort out the C# conversion.

    Sub GetData()
    Application.ScreenUpdating = False
    Dim wdDoc As Document, wdRng As Range
    Set wdDoc = ActiveDocument
    With wdDoc
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Style = "Heading 2"
          .Execute "Feature A^p", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
        End With
        If .Find.Found Then
          Set wdRng = .Duplicate
          wdRng.Collapse 0 'wdCollapseEnd
          .Start = wdRng.End
          With .Find
            .Style = "Heading 1"
            .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
          End With
          If .Find.Found Then
            wdRng.End = .Duplicate.Start - 1
            With wdRng.Duplicate
              With .Find
                .Style = "Heading 2"
                .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
              End With
              If .Find.Found Then
                wdRng.End = .Start - 1
              End If
            End With
          Else
            With .Find
              .Style = "Heading 2"
              .Execute "", False, True, False, False, False, True, 0, True, "", 0, False, False, False, False
            End With
            If .Find.Found Then
              wdRng.End = .Duplicate.Start - 1
            Else
              wdRng.End = wdDoc.Range.End
            End If
          End If
          If Not wdRng Is Nothing Then
            MsgBox wdRng.Text
          End If
        End If
      End With
    End With
    Set wdRng = Nothing: Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 9:00 AM
  • Hi Saurahb,

    >>This code works when same paragraph text present in the document but it is not necessary that same paragraph text should be present. I need All paragraphs after 'Feature A' which have headings below the 'Feature A' paragraph heading.<<

    If you want to get the paragraphs after the specifc text you found, you colud use Range.Next to find the next paragraph after the specific text.

    Here is a sample for your reference:

     public void Main()
            {
                object missing = Type.Missing;
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                Word.Document doc = wordApp.Documents.Open(@"C:\Users\v-fexue\Desktop\TestFind.docx");
                Word.Range rng = doc.Content;
                int startPostion = 0;
                object findText = "Feature A";
                object textFormat = "Heading 2";
    
                rng.Find.ClearFormatting();
                rng.Find.Text = "Feature A";
                rng.Find.set_Style(ref textFormat);
                if (rng.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))
                    startPostion = rng.End;
    
    
                if (rng.Find.Found)
                {
                    Range targetRng=rng.Next(WdUnits.wdParagraph, 1);
                    targetRng.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;
                }
    
            }

    If I misunderstood, please feel free to let me know.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 12, 2014 10:03 AM
    Moderator
  • HI Saurabh

    There's a predefined bookmark in Word that can get the current Heading level plus all paragraphs up to the next occurrence of the same heading level - that's what I understand you to want. In the VBA venacular:
      Doc.Bookmarks("\HeadingLevel").Range

    In C# I believe you need

     object bmName = @"\HeadingLevel";
     Doc.Bookmarks.get_Item(ref bmName).Range


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, May 12, 2014 2:44 PM
    Moderator
  • Hello Cindy,

    Below code is not working. It gives error "The requested member of the collection does not exist."

    object bmName = @"\paragraph1";
    Range rg = Doc.Bookmarks.get_Item(ref bmName).Range;

    Tuesday, May 13, 2014 12:05 PM
  • That's because you have to use the code exactly as a gave it. \paragraph1 is not a built-in Word bookmark.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 13, 2014 2:27 PM
    Moderator