hindersaliva
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.
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
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
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
pls try
Open in new window
Regards