如何使用VBA格式表列字体在Word?



我使用以下代码:

Sub d_FormatTableEndNoteDemo2()
Application.ScreenUpdating = False
Dim Tbl As Table
For Each Tbl In ActiveDocument.StoryRanges(wdEndnotesStory).Tables
With Tbl
.AllowAutoFit = False
.Rows.Alignment = wdAlignRowCenter
.Rows.Height = CentimetersToPoints(0.6)

.Range.Cells.VerticalAlignment = wdCellAlignVerticalTop
.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft

.Columns(1).Width = CentimetersToPoints(1#)
.Columns(2).Width = CentimetersToPoints(5#)
.Columns(3).Width = CentimetersToPoints(11#)

End With
Next
Application.ScreenUpdating = True
End Sub

但是我不知道如何设置第三列的字体= hidden。请帮帮我。

除Excel外,您无法在Word中访问列范围。在这种情况下,您需要遍历特定列的所有单元格:

Public Sub hideTextInColumn(tbl As Table, columnIndex As Long)
Dim c As Cell
For Each c In tbl.Columns(columnIndex).Cells
c.Range.Font.Hidden = True
Next
End Sub

你可以称之为子hideTextInColumn tbl, 3

在你的例程中:

Sub d_FormatTableEndNoteDemo2()
Application.ScreenUpdating = False
Dim Tbl As Table
For Each Tbl In ActiveDocument.StoryRanges(wdEndnotesStory).Tables
With Tbl
.AllowAutoFit = False
.Rows.Alignment = wdAlignRowCenter
.Rows.Height = CentimetersToPoints(0.6)

.Range.Cells.VerticalAlignment = wdCellAlignVerticalTop
.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft

.Columns(1).Width = CentimetersToPoints(1#)
.Columns(2).Width = CentimetersToPoints(5#)
.Columns(3).Width = CentimetersToPoints(11#)
'---- new
hideTextInColumn tbl, 3
'----

End With
Next
Application.ScreenUpdating = True
End Sub
``

下面的子格式为表格第3列中的单元格:

Sub FormatTableColumn()
Dim tbl As Word.Table
Set tbl = ActiveDocument.Tables(1)
tbl.Columns(3).Select
With Selection.Font
.Name = "Times New Roman"
.Size = 10
.Hidden = True
End With
End Sub

最新更新