VBA程序 在Word文档中将表格中空白单元格填充为“无”

打开宏编辑器功能,输入以下代码,然后编译执行。

Sub 填充文档中所有空白单元格()
 Dim oCell As Cell
 For i=1 To ActiveDocument.Tables.Count
 For Each oCell In ActiveDocument.Tables(i).Range.Cells
  If oCell.Range.Text = Chr(13) & Chr(7) Then
    With oCell.Range.Font
        .NameFarEast = "宋体"
        .NameAscii = "宋体"
        .NameOther = "宋体"
        .Size = 12
         oCell.Range.Text = "无"
         oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
         oCell.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
    End With
    End If
 Next
 Next
End Sub

如果仅对单个表格进行空白单元格填充,使用以下vba代码

Sub 单个表格填充空白单元格()
 Dim oCell As Cell
 For Each oCell In Selection.Tables(1).Range.Cells
  If oCell.Range.Text = Chr(13) & Chr(7) Then
    With oCell.Range.Font
        .NameFarEast = "宋体"
        .NameAscii = "宋体"
        .NameOther = "宋体"
        .Size = 12
         oCell.Range.Text = "无"
         oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
         oCell.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter 
    End With
    End If
 Next
End Sub

你可能感兴趣的:(Office办公软件)