Consulting

Results 1 to 5 of 5

Thread: For Each cell in Tables if cell has table within it (Nested Table) Next

  1. #1
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location

    For Each cell in Tables if cell has table within it (Nested Table) Next

    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:

    HTML Code:
    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
            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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    Thanks Greg! Again and again and again!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular pk247's Avatar
    Joined
    Feb 2014
    Posts
    64
    Location
    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

Posting Permissions

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