|
表格上大于一行的左对齐,小于一行的居中,用VBA怎么调?只能全部居中或左对齐,怎么修改程序
Sub TableProcess_Update()
'表格处理 -> 光标在表格外处理所有表格;否则当前表格(选定则选区表格)
Dim r As Range, t As Table, c As Cell, i As Paragraph, a As Row, x&, y&, z&, j&, k&, e&, n&
'PaperSetup
With ActiveDocument
.Fields.Unlink
.ConvertNumbersToText
.Content.Find.Execute "^l", , , 0, , , , , , "^p", 2
End With
With Selection
If .Type = wdSelectionIP Then
If .Information(wdWithInTable) = True Then
Set t = .Tables(1)
Set r = .Tables(1).Range
Else
Set r = ActiveDocument.Content
End If
Else
Set r = .Range
End If
End With
For Each t In r.Tables
With t
'取消环绕/左对齐/左缩进
With .Rows
.WrapAroundText = False
.Alignment = wdAlignRowCenter
.LeftIndent = CentimetersToPoints(0)
.HeightRule = wdRowHeightAtLeast
.Height = CentimetersToPoints(0.9)
End With
'清除格式
With .Range
.Next.InsertParagraphBefore
.MoveEnd
.Select
CommandBars.FindControl(ID:=122).Execute
Selection.ClearFormatting
.MoveEnd 1, -1
With .Font
.NameFarEast = "仿宋_GB2312" '字体
.Size = 12 '10.5五号,12小四,14四号
.Color = RGB(0, 0, 0) '字体颜色
.Kerning = 0
.DisableCharacterSpaceGrid = True
End With
With .ParagraphFormat
.CharacterUnitFirstLineIndent = 0 '首行缩进
.Alignment = wdAlignParagraphCenter '居中对齐
.LineSpacing = LinesToPoints(1.15) '行间距
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
End With
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
.Next.Delete
'判断表格是否规则(e=1=规则/e=0=不规则)
x = .Information(wdEndOfRangeRowNumber)
y = .Information(wdEndOfRangeColumnNumber)
z = .Cells.Count
End With
If x <> 1 Then
If z = x * y Then
For k = 1 To y
For j = 1 To x - 1
If .Cell(j + 1, k).Width = .Cell(j, k).Width Then e = 1 Else e = 0
If e = 0 Then Exit For
Next j
If e = 0 Then Exit For
Next k
Else
e = 0
End If
Else
e = 1
End If
'删除空段
For Each c In .Range.Cells
For Each i In c.Range.Paragraphs
If Asc(i.Range) = 13 And Len(i.Range) = 1 Then i.Range.Delete
Next
With c.Range.Paragraphs
If .Count > 1 And Len(.Last.Range) = 2 Then .Last.Previous.Range.Characters.Last.Delete
End With
Next
'表头加粗
.Cell(1, 1).Select
With Selection
.SelectRow
With .Font
.Bold = True '加粗
End With
With .Range.Find
.Execute "^w", , , 0, , , , , , "", 2
.Execute " ", , , 0, , , , , , "", 2
End With
End With
If e = 1 Then
'删除序号
If .Cell(1, 1).Range Like "序号*" Then
ActiveDocument.Range(Start:=.Cell(2, 1).Range.Start, End:=.Cell(x, 1).Range.End).Delete
End If
'删除空行
For Each a In .Rows
If Len(Replace(Replace(a.Range.Text, vbCr, ""), Chr(7), "")) = 0 Then a.Delete
Next
'序号自动
If .Cell(1, 1).Range Like "序号*" Then
ActiveDocument.Range(Start:=.Cell(2, 1).Range.Start, End:=.Cell(x, 1).Range.End).Select
n = 0
For Each c In Selection.Cells
n = n + 1
c.Range.Text = n
Next
End If
End If
'边距
.LeftPadding = CentimetersToPoints(0.02)
.RightPadding = CentimetersToPoints(0.02)
.AutoFitBehavior (wdAutoFitContent)
.Select
.AutoFitBehavior (wdAutoFitWindow)
End With
Next
' Selection.HomeKey Unit:=wdStory
End Sub
|
|