janaboo13
04-05-2011, 07:57 AM
I'm pretty new to VBA and thanks to this forum, I am learning so much, but I'm stumped with this current issue. We are using a component db management system and have found that creating tables for some of our content that includes graphics is the solution of choice for formatting reasons. We then publish to Word. This process, however, results in a large number of tables in a document. For a 50-page document, the code below works great (even though I hate stepping through ALL the tables.) Besides the tables that are used for formatting reasons, we have a number of tables that are formatted with borders. I would like to search for only those tables to make sure that the correct borders are applied in the resulting Word doc.
We're getting ready to create a much larger doc set and the thought of stepping through each table in those documents is daunting, at best.
I need some help modifying the code below to only search for tables that have specific table heading styles (Table Heading L, Table Heading R, etc.) so that I don't have to search through every table in the document.
If anyone has suggestions on how to do that, I would sooooo appreciate the help!! If you need to see a document to clarify what I need to do, please let me know and I'll post.
Here's the code I'm using now...
'This macro sets the top table border to 1 pt and
' all other horizontable table borders to .5 pt
Sub FixTableBorders()
' Runs through all tables from a user defined start point asking user to indicate whether formatting is to be applied.
' This determines the start point
Response = MsgBox("Start here? Select No to start at beginning of document.", vbYesNoCancel)
If Response = vbYes Then
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables beyond start point.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables after this position"
End
End If
ElseIf Response = vbNo Then
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables in the document.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables in this document"
End
End If
Else
End
End If
' This finds the next table from start point and starts the formatting
Do
Response = MsgBox("Fix border on this table?", vbYesNoCancel)
If Response = vbYes Then
' This selects the table
With Selection
.SelectColumn
.SelectRow
End With
' This formats the table
With Selection.Rows.Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
End With
With Selection.Rows.Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
With Selection.Rows.Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
' This collapses the selection and moves to the next table or displays the end of tables message
Selection.Collapse wdCollapseEnd
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
ElseIf Response = vbNo Then
' Selects Table and then collapses to point beyond the table
With Selection
.SelectColumn
.SelectRow
End With
Selection.Collapse wdCollapseEnd
' Looks for next table and loops if found or ends macro
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
Else
End
End If
Loop
MsgBox "End of tables in this document"
End Sub
We're getting ready to create a much larger doc set and the thought of stepping through each table in those documents is daunting, at best.
I need some help modifying the code below to only search for tables that have specific table heading styles (Table Heading L, Table Heading R, etc.) so that I don't have to search through every table in the document.
If anyone has suggestions on how to do that, I would sooooo appreciate the help!! If you need to see a document to clarify what I need to do, please let me know and I'll post.
Here's the code I'm using now...
'This macro sets the top table border to 1 pt and
' all other horizontable table borders to .5 pt
Sub FixTableBorders()
' Runs through all tables from a user defined start point asking user to indicate whether formatting is to be applied.
' This determines the start point
Response = MsgBox("Start here? Select No to start at beginning of document.", vbYesNoCancel)
If Response = vbYes Then
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables beyond start point.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables after this position"
End
End If
ElseIf Response = vbNo Then
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
' This gives warning message if there are no tables in the document.
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "There are no tables in this document"
End
End If
Else
End
End If
' This finds the next table from start point and starts the formatting
Do
Response = MsgBox("Fix border on this table?", vbYesNoCancel)
If Response = vbYes Then
' This selects the table
With Selection
.SelectColumn
.SelectRow
End With
' This formats the table
With Selection.Rows.Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
End With
With Selection.Rows.Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
With Selection.Rows.Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
' This collapses the selection and moves to the next table or displays the end of tables message
Selection.Collapse wdCollapseEnd
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
ElseIf Response = vbNo Then
' Selects Table and then collapses to point beyond the table
With Selection
.SelectColumn
.SelectRow
End With
Selection.Collapse wdCollapseEnd
' Looks for next table and loops if found or ends macro
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
If Selection.Information(wdWithInTable) = True Then
Else
MsgBox "End of tables in this document"
End
End If
Else
End
End If
Loop
MsgBox "End of tables in this document"
End Sub