0
点赞
收藏
分享

微信扫一扫

word vba 操作表格, 设置表格的样式

一、新建和删除表格样式

Sub 表格样式()
  Dim oldstyle As Style, tablestyle As Style
'  On Error Resume Next
  '删除表格样式
  'ActiveDocument.Styles("mytablestyle").Delete
  
  '在创建之前,判断样式是否存在,如果存在自定义的样式则删除
  For Each oldstyle In ActiveDocument.Styles
    If oldstyle.NameLocal = "mytablestyle" Then
      oldstyle.Delete
    End If
  Next oldstyle
  
  '创建一个表格样式
  Set tablestyle = ActiveDocument.Styles.Add( _
  Name:="mytablestyle", Type:=wdStyleTypeTable)
'  WdStyleType 枚举
'  指定要应用的样式类型?
'  名称                    值    说明
'  wdStyleTypeParagraph    1    段落样式。
'  wdStyleTypeCharacter    2    正文字符样式。
'  wdStyleTypeTable        3    表格样式。
'  wdStyleTypeList         4    列表样式。
End Sub

二、设置一个表格样式

Sub 表格样式()
  Dim oldstyle As Style, tablestyle As Style
'  On Error Resume Next
  '删除表格样式
  'ActiveDocument.Styles("mytablestyle").Delete
  
  '判断样式是否存在,如果存在自定义的样式则删除
  For Each oldstyle In ActiveDocument.Styles
    If oldstyle.NameLocal = "mytablestyle" Then
      oldstyle.Delete
    End If
  Next oldstyle
  
  '创建一个表格样式
  Set tablestyle = ActiveDocument.Styles.Add( _
  Name:="mytablestyle", Type:=wdStyleTypeTable)
'  tablestyle.BaseStyle = "普通表格"
  
'  WdStyleType 枚举
'  指定要应用的样式类型?
'  名称                    值    说明
'  wdStyleTypeParagraph    1    段落样式。
'  wdStyleTypeCharacter    2    正文字符样式。
'  wdStyleTypeTable        3    表格样式。
'  wdStyleTypeList         4    列表样式。
'------------------------------------------------------
'设置样式的字体格式
  With tablestyle.Font
    .NameFarEast = "宋体"
    .NameAscii = "Times New Roman"
    .Bold = False
    .Italic = False
    .Size = 9
    .ColorIndex = wdBlack
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorBlack
    .EmphasisMark = wdEmphasisMarkNone
    .StrikeThrough = False
    .DoubleStrikeThrough = False
    .Superscript = False
    .Subscript = False
    .SmallCaps = False
    .AllCaps = False
    .Hidden = False
  End With
'-------------------------------------------
'设置样式的段落格式
  With tablestyle.ParagraphFormat
    .Alignment = wdAlignParagraphRight
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 0
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LeftIndent = 0
    .RightIndent = 0
    .FirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    
    .WidowControl = False
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .WordWrap = False
    .NoLineNumber = False
    
    .FarEastLineBreakControl = False
    .WordWrap = False
    .HangingPunctuation = False
    
    .HalfWidthPunctuationOnTopOfLine = False
    .AddSpaceBetweenFarEastAndAlpha = False
    .AddSpaceBetweenFarEastAndDigit = False
  End With
'------------------------------------------------
  '设置表格属性
  With tablestyle.Table
    .TopPadding = CentimetersToPoints(0)
    .BottomPadding = CentimetersToPoints(0)
    .LeftPadding = CentimetersToPoints(0.2)
    .RightPadding = CentimetersToPoints(0.2)
    .Spacing = 0
    .LeftIndent = CentimetersToPoints(0)
    
    With .Shading
      .Texture = wdTextureNone
      .ForegroundPatternColor = wdColorAutomatic
      .BackgroundPatternColor = wdColorAutomatic
    End With
    
    .Borders(wdBorderTop).LineStyle = wdLineStyleDouble
    .Borders(wdBorderBottom).LineStyle = wdLineStyleDouble
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
    .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
    .Borders.Shadow = False
  End With
End Sub
举报

相关推荐

0 条评论