DaneMartin
07-02-2015, 04:49 AM
Hello everyone
I have dabbled a bit with vba for Excel, but I am quite new to vba in word.
I have tried to make a piece of code to format all tables in a document. It selects only tables with the caption "Tabel" since I also have text boxes with another caption which are strictly speaking 1-field tables.
The code worked at some point, but I must have done something wrong, because now it seems that nothing happens between "If" and "End if".
I am looking forward to any good advice on this. Thanks in advance!
Sub FormatTables()
Application.ScreenUpdating = False
Application.ActiveWindow.View.ShowFieldCodes = True
'Always start at the top of the document
Selection.HomeKey Unit:=wdStory
Dim myTable As Table
For Each myTable In ActiveDocument.Tables
myTable.Select
'Makes sure only tables with the caption “Tabel” are selected
With Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="seq tabel"
End With
If Selection.Find.Found Then
Selection.Tables(1).Select
Selection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
Selection.ParagraphFormat.KeepWithNext = True
Selection.Rows.HeightRule = wdRowHeightAtLeast
Selection.Rows.Height = CentimetersToPoints(0.4)
With Selection.Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth150pt
.Color = wdColorAutomatic
End With
With Selection.Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth150pt
.Color = wdColorAutomatic
End With
End If
Next myTable
Application.ActiveWindow.View.ShowFieldCodes = False
Call UpdateAllFields
Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = True
End Sub
Sub UpdateAllFields()
Dim myRange
Set myRange = Selection.Range
Selection.WholeStory
Selection.Fields.Update
myRange.Select
End Sub
I have dabbled a bit with vba for Excel, but I am quite new to vba in word.
I have tried to make a piece of code to format all tables in a document. It selects only tables with the caption "Tabel" since I also have text boxes with another caption which are strictly speaking 1-field tables.
The code worked at some point, but I must have done something wrong, because now it seems that nothing happens between "If" and "End if".
I am looking forward to any good advice on this. Thanks in advance!
Sub FormatTables()
Application.ScreenUpdating = False
Application.ActiveWindow.View.ShowFieldCodes = True
'Always start at the top of the document
Selection.HomeKey Unit:=wdStory
Dim myTable As Table
For Each myTable In ActiveDocument.Tables
myTable.Select
'Makes sure only tables with the caption “Tabel” are selected
With Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="seq tabel"
End With
If Selection.Find.Found Then
Selection.Tables(1).Select
Selection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
Selection.ParagraphFormat.KeepWithNext = True
Selection.Rows.HeightRule = wdRowHeightAtLeast
Selection.Rows.Height = CentimetersToPoints(0.4)
With Selection.Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth150pt
.Color = wdColorAutomatic
End With
With Selection.Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth150pt
.Color = wdColorAutomatic
End With
End If
Next myTable
Application.ActiveWindow.View.ShowFieldCodes = False
Call UpdateAllFields
Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = True
End Sub
Sub UpdateAllFields()
Dim myRange
Set myRange = Selection.Range
Selection.WholeStory
Selection.Fields.Update
myRange.Select
End Sub