PDA

View Full Version : [SOLVED:] For Each cell in Tables if cell has table within it (Nested Table) Next



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

gmaxey
11-16-2016, 07:34 PM
If ocell.Tables.Count > 0 Then
'This doesn't work despite there being tables within some cells I don't get the msg
MsgBox "found one"
Else
With ocell.Range

pk247
11-17-2016, 05:42 PM
Thanks Greg! Again and again and again!

gmaxey
11-18-2016, 06:17 AM
If you begin to thing think that Word behaves logically you can always dispel that line of thought with your table and code like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCell As Cell
For Each oCell In ActiveDocument.Tables(1).Range.Cells
oCell.Select
If oCell.Tables.Count > 0 Then MsgBox "found one"
MsgBox Selection.Tables.Count
oCell.Range.Select
If oCell.Range.Tables.Count > 0 Then MsgBox "found one"
MsgBox Selection.Tables.Count
Next
lbl_Exit:
Exit Sub
End Sub

pk247
11-21-2016, 04:47 PM
I think you've spotted exactly what I'm doing wrong Greg. I need to learn to take a step back and think about what I'm trying to achieve and then try writing the code.

I'm pretty new to this so it'll come eventually. I checked out your website - Wow! Seems to me a much more exciting life than my desk job!

Thank you,

Paul, IRELAND