Consulting

Results 1 to 6 of 6

Thread: What continuous section contains my keyword?

  1. #1
    VBAX Regular
    Joined
    Nov 2013
    Location
    San Francisco Bay Area
    Posts
    27
    Location

    What continuous section contains my keyword?

    Word 2010

    I have inserted paragraphs into a Word document. I enclosed it in sections. I now want to find the section index that contains the keyword and delete the section and its contents. The keyword will only appear in one section. Is there sample code that show how to do it? Thanks

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: http://www.msofficeforums.com/word-v...e-section.html
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Nov 2013
    Location
    San Francisco Bay Area
    Posts
    27
    Location

    Solution...

    This is what I did to remove the sections and text I inserted into the document.


    strSearchString = "Inst#: " & ReportInstance
     
    With Selection.Find
     
        .ClearFormatting 
        .Replacement.ClearFormatting
        .Text = strSearchString
     
        .Replacement.Text = ""
     
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
     
        ' what section am I in?
        If .Found = True Then
     
            intSectionCount = ActiveDocument.Range(0, Selection.Range.End).Sections.Count
     
     
            ActiveDocument.Sections(intSectionCount).Range.Delete
            ActiveDocument.Sections(intSectionCount - 1).Range.Delete
     
        End If
     
    End With

  4. #4
    VBAX Regular
    Joined
    Nov 2013
    Location
    San Francisco Bay Area
    Posts
    27
    Location
    Correction...

    You should not do this line

    ActiveDocument.Sections(intSectionCount - 1).Range.Delete

    It will delete the paragraph above the insertion.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Simpler and more efficient:
    strSearchString = "Inst#: " & ReportInstance
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = strSearchString
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      If .Find.Found = True Then
        .End = .Duplicate.Sections(1).Range.End
        .Start = .Duplicate.Sections(1).Range.Start - 1
        .Duplicate.Text = vbNullString
      End If
    End With
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Regular
    Joined
    Nov 2013
    Location
    San Francisco Bay Area
    Posts
    27
    Location
    That works fantastic and I can remove the second section with losing the existing data. Still testing to do but Thank you, Thank you, Thank you. I will also study to see exactly what is going on. I need to learn why mine goofs up an existing paragraph and yours does not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •