none
Search specific text only inside all the tables in a word document RRS feed

  • Question

  • Hi,


    I want to search specific text only inside all the tables in a word document, if it find that word in any where in the table then do two things:

    1. Convert the  first cell body text to heading 1.

    2. Convert that table into text.


    Rest of the tables which does not have that word should remain same.


    I tried following code which solve only part of my question: first it search for the text and then convert table into text.


    If there is more efficient way to write the same code and solve my both the issues then please try and reply.


    Sub Find_Text_in_table()

        Selection.Find.ClearFormatting
        
        Dim tbl As Table
    For Each tbl In ActiveDocument.Tables

        With Selection.Find
            .Text = "urgent"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

        Do While Selection.Find.Execute

            If Selection.Information(wdWithInTable) Then
            tbl.ConvertToText wdSeparateByTabs, True
            End If
        Loop
        Next tbl
    End Sub


    Thank you for your time and efforts.


    Ajay

    Thursday, April 28, 2016 5:01 PM

Answers

  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = InputBox("What is the Text to Find")
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .Information(wdWithInTable) = True Then
            With .Tables(1)
                Set Rng = .Cell(1, 1).Range
                .ConvertToText wdSeparateByTabs, True
            End With
            Rng.Style = wdStyleHeading1
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    Do note that, if there is more than one cell on the first row, using the wdSeparateByTabs parameter with ConvertToText means the entire row must be converted to Heading1. If you want only that cell converted to Heading1, you'll need to replace:
          Rng.Style = wdStyleHeading1
    with something like:
          With Rng
            .MoveEndUntil vbTab, wdForward
            .InsertAfter vbCr
            .Style = wdStyleHeading1
          End With

    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, April 29, 2016 6:24 AM Cross-post
    • Proposed as answer by David_JunFeng Wednesday, May 4, 2016 1:18 AM
    • Marked as answer by David_JunFeng Thursday, May 5, 2016 9:53 AM
    Friday, April 29, 2016 3:22 AM

All replies

  • Sorry, just realized this code is not working properly. 

    Ajay

    Thursday, April 28, 2016 6:37 PM
  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = InputBox("What is the Text to Find")
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .Information(wdWithInTable) = True Then
            With .Tables(1)
                Set Rng = .Cell(1, 1).Range
                .ConvertToText wdSeparateByTabs, True
            End With
            Rng.Style = wdStyleHeading1
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    Do note that, if there is more than one cell on the first row, using the wdSeparateByTabs parameter with ConvertToText means the entire row must be converted to Heading1. If you want only that cell converted to Heading1, you'll need to replace:
          Rng.Style = wdStyleHeading1
    with something like:
          With Rng
            .MoveEndUntil vbTab, wdForward
            .InsertAfter vbCr
            .Style = wdStyleHeading1
          End With

    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, April 29, 2016 6:24 AM Cross-post
    • Proposed as answer by David_JunFeng Wednesday, May 4, 2016 1:18 AM
    • Marked as answer by David_JunFeng Thursday, May 5, 2016 9:53 AM
    Friday, April 29, 2016 3:22 AM
  • >>>
    1. Convert the  first cell body text to heading 1.
    2. Convert that table into text.
    <<<

    According to your description, you could refer to below code:
    Sub Demo()
       For Each tbl In ActiveDocument.Tables
          If SearchTableWithWord(tbl, "urgent") Then
            Set Rng = tbl.Cell(1, 1).Range
            tbl.ConvertToText wdSeparateByTabs, True
            Rng.Style = wdStyleHeading1
          End If
       Next tbl
    End Sub
    
    Function SearchTableWithWord(ByVal searchTable As Table, searchWord As String) As Boolean
       For Each oRow In searchTable.Rows
          For Each ocell In oRow.Cells
              sCellText = ocell.Range
              sCellText = Left$(sCellText, Len(sCellText) - 2)
              If searchWord = sCellText Then
                 SearchTableWithWord = True
                 Exit Function
              End If
          Next ocell
       Next oRow
    
       SearchTableWithWord = False
       Exit Function
    End Function

    Friday, April 29, 2016 5:13 AM
  • Hi David, if you're going to loop through all tables, a much more efficient function would be useful. For example:

    Function SearchTableWithWord(ByVal searchTable As Table, searchWord As String) As Boolean
    SearchTableWithWord = False
    With searchTable.Range.Find
        .Text = searchWord
        .MatchWholeWord = True
        .Wrap = wdFindStop
        .Execute
        If .Found Then SearchTableWithWord = True
    End With
    End Function

    Toggling Application.ScreenUpdating, as my code does, will also make the code execute much more quickly.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, April 29, 2016 6:15 AM
  • Hi, macropod

    Thanks for your advice.

    Tuesday, May 3, 2016 5:11 AM