pk247
11-16-2016, 04:52 PM
Hi All,
I don't know where I'd be without this forum... I'm hoping someone can help me with this bit of code I'm working on please? (2 hours later and I'm not making headway)
I'd like to be able to loop through the cells of each table (after table 4) in the active document and perform a find replace in each individual cell. However, some cells contain a table within them and if this is the case then do nothing in that "outer" cell and move on to the next cell i.e. skip any cells that contain tables.
I would have thought my code below would work but I'm not an experienced programmer by any means and can only fumble my way round with lots of testing until I think it works... Although I'm getting a little better thanks to this forum!!
Here's my code so far:
Sub FindReplaceSkipNestedTables()
Dim oTbl As Word.Table
Dim ocell As Word.Cell
Dim rw As Row
Dim lngIndex As Long
Application.ScreenUpdating = False
For lngIndex = 4 To ActiveDocument.Tables.Count
Set oTbl = ActiveDocument.Tables(lngIndex)
For Each ocell In oTbl.Range.Cells
If ocell.Range.Tables.NestingLevel = 2 Then
'This doesn't work despite there being tables within some cells I don't get the msg
MsgBox "found one"
ElseIf ocell.Range.Tables.NestingLevel = 1 Then
With ocell.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "TESTFIND"
.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
.Text = "TESTREPLACE"
End If
If .End = ActiveDocument.Range.End Then Exit Sub
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End If
Next ocell
Next lngIndex
Application.ScreenUpdating = True
End Sub
Hopefully you can see what I'm trying to achieve here?
Many thanks,
Paul, IRELAND
I don't know where I'd be without this forum... I'm hoping someone can help me with this bit of code I'm working on please? (2 hours later and I'm not making headway)
I'd like to be able to loop through the cells of each table (after table 4) in the active document and perform a find replace in each individual cell. However, some cells contain a table within them and if this is the case then do nothing in that "outer" cell and move on to the next cell i.e. skip any cells that contain tables.
I would have thought my code below would work but I'm not an experienced programmer by any means and can only fumble my way round with lots of testing until I think it works... Although I'm getting a little better thanks to this forum!!
Here's my code so far:
Sub FindReplaceSkipNestedTables()
Dim oTbl As Word.Table
Dim ocell As Word.Cell
Dim rw As Row
Dim lngIndex As Long
Application.ScreenUpdating = False
For lngIndex = 4 To ActiveDocument.Tables.Count
Set oTbl = ActiveDocument.Tables(lngIndex)
For Each ocell In oTbl.Range.Cells
If ocell.Range.Tables.NestingLevel = 2 Then
'This doesn't work despite there being tables within some cells I don't get the msg
MsgBox "found one"
ElseIf ocell.Range.Tables.NestingLevel = 1 Then
With ocell.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "TESTFIND"
.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
.Text = "TESTREPLACE"
End If
If .End = ActiveDocument.Range.End Then Exit Sub
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End If
Next ocell
Next lngIndex
Application.ScreenUpdating = True
End Sub
Hopefully you can see what I'm trying to achieve here?
Many thanks,
Paul, IRELAND