WordVBAで表の操作


アクティブなページのテーブルを選択

'アクティブなページのテーブルを選択する。
    With ActiveDocument.Bookmarks("\Page").Range.Tables
     If .Count = 0 Then Exit Sub
        .Item(1).Select
    End With

表内の文字サイズ,デザイン,行間の指定

'例はサイズ7p , デザインは"MS Pゴシック"
    With Selection.Range.Font
        .Size = 7
        .Name = "MS Pゴシック"
    End With

'例は行間8p固定
    With Selection.ParagraphFormat
        .LineSpacingRule = wdLineSpaceExactly
        .LineSpacing = 8
    End With

表内の全セルを垂直方向中央揃え

 Dim rowMax As Long, i As Long
 rowMax = ActiveDocument.Bookmarks("\Page").Range.Tables.Item(1).Rows.Count
 For i = 1 To rowMax
    With ActiveDocument.Bookmarks("\Page").Range.Tables.Item(1).Rows(i)
        .Cells.VerticalAlignment = wdCellAlignVerticalCenter 
    End With
Next

行の高さを変更

'1行目を7.5mmに変更
Call ActiveDocument.Bookmarks("\Page").Range.Tables.Item(1).Rows(1).SetHeight(MillimetersToPoints(7.5), wdRowHeightExactly)

列の幅を変更

'1列目を8mmに変更
Call ActiveDocument.Bookmarks("\Page").Range.Tables.Item(1).Columns(1).SetWidth(MillimetersToPoints(8), wdAdjustNone)