PDA

View Full Version : [SOLVED:] Find within table



Pawel
08-01-2016, 01:05 AM
Hi guys,
I've got this problem:
I have a Word document with 10 different tables in it, all of them on separate pages. As for now I have a macro that finds and highlights a certain value contained within those tables, but I also need it to add a MsgBox that says in which table (table.count) this certain value exists.
Current Code:

Sub RMT()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "--"
.Forward = True
.Wrap = wdFindAsk
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
End If
Loop
End Sub


Anyone help?

Pawel
08-01-2016, 04:01 AM
Already got what I was looking for:) :

Sub RMT()

Selection.Find.ClearFormatting
With Selection.Find
.Text = "--"
.Forward = True
.Wrap = wdFindAsk
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
End If
Dim iTable&
With Selection
For iTable = 1 To ActiveDocument.Tables.Count
If (.Range.Start >= ActiveDocument.Tables(iTable).Range.Start) And _
(.Range.End <= ActiveDocument.Tables(iTable).Range.End) Then
MsgBox "It's in table " & iTable
Exit For
End If
Next iTable
End With
Loop
End Sub



I would like just to change MsgBox so it would show the name of the Table defined by its number. for example
1 - "***"
2 - "YYY"
3- "ZZZ"
and so on... So the MsgBox would actually be "It's in table YYY" or "It's in table ***"

Please help someone.

gmayor
08-01-2016, 04:39 AM
You would need to tell the macro which text each table number referred to e.g.


Sub RMT()
Dim iTable As Integer
Dim strTable As String
Selection.Find.ClearFormatting
With Selection.Find
.Text = "--"
.Forward = True
.Wrap = wdFindAsk
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
End If
With Selection
For iTable = 1 To ActiveDocument.Tables.Count
If (.Range.start >= ActiveDocument.Tables(iTable).Range.start) And _
(.Range.End <= ActiveDocument.Tables(iTable).Range.End) Then
Select Case iTable
Case 1: strTable = "***"
Case 2: strTable = "YYY"
Case 3: strTable = "ZZZ"
End Select
MsgBox "It's in table " & strTable
Exit For
End If
Next iTable
End With
Loop
End Sub

Pawel
08-01-2016, 05:16 AM
That's exactly what I needed. Thank you gmayor.

I have one more issue though: how and where do I put a statement of what happens if no "--" text is found in document tables?

p45cal
08-01-2016, 07:34 AM
Not being very famliar with Word vba (but since you asked via PM) I would guess maybe the following:
Sub RMT()
Dim iTable As Integer
Dim strTable As String
Dim Found As Boolean

Selection.Find.ClearFormatting
Found = False
With Selection.Find
.Text = "--"
.Forward = True
.Wrap = wdFindAsk
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
End If
With Selection
For iTable = 1 To ActiveDocument.Tables.Count
If (.Range.Start >= ActiveDocument.Tables(iTable).Range.Start) And _
(.Range.End <= ActiveDocument.Tables(iTable).Range.End) Then
Select Case iTable
Case 1: strTable = "***"
Case 2: strTable = "YYY"
Case 3: strTable = "ZZZ"
End Select
MsgBox "It's in table " & strTable
Exit For
End If
Next iTable
End With
Loop
If Not Found Then MsgBox "'--' not found within any table."
End Sub

gmaxey
08-01-2016, 07:36 AM
To each his own but I also find it easier to work with a range object vice the selection:


Sub RMTX()
Dim oCol As New Collection
Dim oRng As Word.Range, oRngCounter As Range
Dim strReport As String
Dim lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "--"
.Forward = True
While .Execute
If oRng.Information(wdWithInTable) Then
oRng.Shading.Texture = wdTextureNone
oRng.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Shading.BackgroundPatternColor = wdColorYellow
Set oRngCounter = oRng
oRngCounter.Start = ActiveDocument.Range.Start
On Error Resume Next
oCol.Add CStr(oRngCounter.Tables.Count), CStr(oRngCounter.Tables.Count)
On Error GoTo 0
oRng.Collapse wdCollapseEnd
End If
Wend
End With
Select Case oCol.Count
Case 0: MsgBox "Text -- not found in document tables."
Case 1: MsgBox "Text -- found in table " & oCol.Item(1)
Case 2: MsgBox "Text -- found in tables " & oCol.Item(1) & " and " & oCol.Item(2)
Case Else
For lngIndex = 1 To oCol.Count - 2
strReport = strReport & oCol.Item(lngIndex) & ", "
Next lngIndex
strReport = strReport & oCol.Item(oCol.Count - 1) & " and " & oCol.Item(oCol.Count)
MsgBox "Text -- found in tables " & strReport & "."
End Select
lbl_Exit:
Exit Sub
End Sub

Pawel
08-01-2016, 07:55 AM
p45cal - Thanks for your help, unfortunately The msgbox you added appears even if there is "--" value, I guess it's because of the loop. But still many thanks for your effort.
gmaxey - Thank you.That's almost what I was aiming for, but as I described above I need to assign names to those table numbers, how do I do that?

p45cal
08-01-2016, 08:06 AM
Oops, I missed out a Found = True. Try this.
Sub RMT()
Dim iTable As Integer
Dim strTable As String
Dim Found As Boolean

Selection.Find.ClearFormatting
Found = False
With Selection.Find
.Text = "--"
.Forward = True
.Wrap = wdFindAsk
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
End If
With Selection
For iTable = 1 To ActiveDocument.Tables.Count
If (.Range.Start >= ActiveDocument.Tables(iTable).Range.Start) And _
(.Range.End <= ActiveDocument.Tables(iTable).Range.End) Then
Select Case iTable
Case 1: strTable = "***"
Case 2: strTable = "YYY"
Case 3: strTable = "ZZZ"
End Select
MsgBox "It's in table " & strTable
Found = True
Exit For
End If
Next iTable
End With
Loop
If Not Found Then MsgBox "'--' not found within any table."
End Sub
I thought you'd already got the assignment of names to tables thing?

gmaxey
08-01-2016, 08:18 AM
Sub RMTX()
Dim arrNames() As String
Dim oCol As New Collection
Dim oRng As Word.Range, oRngCounter As Range
Dim strReport As String
Dim lngIndex As Long
arrNames() = Split("AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,III,JJJ", ",")
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "--"
.Forward = True
While .Execute
If oRng.Information(wdWithInTable) Then
oRng.Shading.Texture = wdTextureNone
oRng.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Shading.BackgroundPatternColor = wdColorYellow
Set oRngCounter = oRng
oRngCounter.Start = ActiveDocument.Range.Start
On Error Resume Next
oCol.Add arrNames(oRngCounter.Tables.Count - 1), CStr(oRngCounter.Tables.Count)
On Error GoTo 0
oRng.Collapse wdCollapseEnd
End If
Wend
End With
Select Case oCol.Count
Case 0: MsgBox "Text -- not found in document tables."
Case 1: MsgBox "Text -- found in table " & oCol.Item(1)
Case 2: MsgBox "Text -- found in tables " & oCol.Item(1) & " and " & oCol.Item(2)
Case Else
For lngIndex = 1 To oCol.Count - 2
strReport = strReport & oCol.Item(lngIndex) & ", "
Next lngIndex
strReport = strReport & oCol.Item(oCol.Count - 1) & " and " & oCol.Item(oCol.Count)
MsgBox "Text -- found in tables " & strReport & "."
End Select
lbl_Exit:
Exit Sub
End Sub

Pawel
08-02-2016, 01:43 AM
Both of those macros work perfect. Thank you so much p45cal and gmaxey :)