萬盛學電腦網

 萬盛學電腦網 >> WORD教程 >> WORD使用技巧 >> VBA用循環設置表格集合、對表格邊框的設置

VBA用循環設置表格集合、對表格邊框的設置

   如下的VBA代碼功能,是用循環來操作WORD中的表格集合、設置表格的邊框等屬性。

  Dim i As Table, N As Integer

  On Error Resume Next ’忽略錯誤

  Application.ScreenUpdating = False ’關閉屏幕更新

  For Each i In ActiveDocument.Tables ’在表格中循環

  With i

  .Style = "列表型 4" ’將所有表格設置為"列表型4"的樣式

  With .Borders ‘邊框

  .InsideLineStyle = wdLineStyleSingle ’設置內部邊框線條

  End With

  With .Rows(1).Borders(wdBorderBottom) ‘第一行的底邊框

  .LineStyle = wdLineStyleDouble ‘雙線型

  .LineWidth = wdLineWidth050pt

  .Color = wdColorAutomatic

  End With

  If .Rows.Count > 1 Then ’ 如果表格行數大於1

  If Len(.Cell(2, 1).Range) <= 2 Then ’如果第二行第一列不為空

  With .Rows(2).Shading ’設置底紋

  .Texture = wdTextureNone ’無底底紋

  .ForegroundPatternColor = wdColorAutomatic

  .BackgroundPatternColor = wdColorGray125

  End With

  End If

  End If

  For N = 2 To .Columns.Count ’從第二列到最後一列

  .Columns(N).Select ’單元格對齊方式為中部居中

  Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

  Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter

  Next N

  End With

  Next i

  Application.ScreenUpdating = True

  End Sub

copyright © 萬盛學電腦網 all rights reserved