PDA

View Full Version : [SOLVED:] Search if a string is present in doc, and to identify whether its in any table or not



nkpan
04-04-2018, 11:48 PM
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

gmayor
04-05-2018, 04:14 AM
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

macropod
04-05-2018, 04:15 AM
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

nkpan
04-05-2018, 05:14 AM
Thanks, that was awesome, i was not aware of the word "wdWithInTable" ... its working :clap:

nkpan
04-05-2018, 05:16 AM
Thank you, it was helpful and a new learning too, respect:bow: