Consulting

Results 1 to 5 of 5

Thread: Search if a string is present in doc, and to identify whether its in any table or not

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Location
    MUMBAI
    Posts
    13
    Location

    Search if a string is present in doc, and to identify whether its in any table or not

    Hello all,

    This is my first post of my life .

    I am working on word macro where i need to find if a string is present in the word document or not and if present, then i need to know if its within a table or not.. It needs to highlight if its not in table.

    As of now i am only able to find the string but for identifying the presence in table or not, even after using the below line of code it work sometimes but not all the time.

    If cRange.Tables.Count > 0 then msgbox "Yes"

    Many thanks in advance, if anyone could please help in this regards

    Nkpan

  2. #2
    Maybe something like

    Sub Macro1()
    Dim oRng As Range
    Dim strText As String
        Set oRng = ActiveDocument.Range
        strText = InputBox("Enter the text to find")
        If Not strText = "" Then
            With oRng.Find
                Do While .Execute(FindText:=strText)
                    If oRng.Information(wdWithInTable) = False Then
                        oRng.HighlightColorIndex = wdYellow
                        oRng.Collapse 0
                    End If
                Loop
            End With
        End If
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    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) = False Then .HighlightColorIndex = wdBrightGreen
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Regular
    Joined
    Apr 2018
    Location
    MUMBAI
    Posts
    13
    Location
    Thanks, that was awesome, i was not aware of the word "wdWithInTable" ... its working

  5. #5
    VBAX Regular
    Joined
    Apr 2018
    Location
    MUMBAI
    Posts
    13
    Location
    Thank you, it was helpful and a new learning too, respect

Posting Permissions

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