Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Word VBA - code crashes on some tables, but not others.

When I run this on a document that has some tables with the Style "SectionsToRemove" it crashes on the If statement (line 11). BUT it only crashes on some tables.
Sub HideTablesToBeHidden_SectionsToRemove()

    Dim doc As Document
    
    Set doc = ActiveDocument
    
    intTablesCount = doc.Tables.Count
    
    For i = 1 To intTablesCount
    
            If doc.Tables(i).Range.Style = "SectionsToRemove" Then[/b][/b]
            
            doc.Tables(i).Range.Font.Hidden = True
            
        End If
   
    Next i
    
End Sub

Open in new window


Why could this be?
My colleague suggests that these tables may have originated in an older version of Word.
If so, is there a work around?
Thanks
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Sub HideTablesToBeHidden_SectionsToRemove()

    Dim doc As Document
    
    Set doc = ActiveDocument
    
    intTablesCount = doc.Tables.Count
    
    For Each tbl In ActiveDocument.Tables
        For Each col In tbl.Columns
            For Each c In col.Cells
                If c.Range.Style = "SectionsToRemove" Then 
                    c.Range.Font.Hidden = True
                End If
            Next
        Next
    Next
    
End Sub

Open in new window

Regards
if the tables have irregular patterns then try
Sub HideTablesToBeHidden_SectionsToRemove()

    Dim doc As Document
    
    Set doc = ActiveDocument
    
    intTablesCount = doc.Tables.Count
    
    For Each tbl In ActiveDocument.Tables
        For IdxCol = 1 To tbl.Columns.Count
            For IdxRw = 1 To tbl.Rows.Count
                Set c = Nothing
                On Error Resume Next
                Set c = tbl.Cell(IdxRw, IdxCol)
                On Error GoTo 0
                If Not c Is Nothing Then
                If c.Range.Style = "SectionsToRemove" Then 
                    c.Range.Font.Hidden = True
                End If
                End If
            Next
        Next
    Next
    
End Sub

Open in new window

Avatar of hindersaliva

ASKER

Rgonzo, no it didn't work. I have attached a sample .docx. On this there are several tables. My code crashes only on one of them. I need to know why that is.
The problem can be solved by replacing the table with a new table, but that will be extremely laborious as there are a dozen templates with may be 100 tables on each.
Tables-issue-to-send-to-E-E.docx
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry. I'm closing an old question.
The reason for the crashes was, there were odd styles that were not visible on my client's templates. I think Rgonzo's code helped me solve it.
But I passed the templates back to the client to clean up.
Thanks.