首页 > 其他分享 >word vba 操作表格, 设置表格的样式

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

时间:2022-12-29 18:33:43浏览次数:41  
标签:vba False 表格 样式 tablestyle oldstyle End word

一、新建和删除表格样式

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

标签:vba,False,表格,样式,tablestyle,oldstyle,End,word
From: https://blog.51cto.com/shenjiren/5978700

相关文章

  • 如何在表格中写正则表达式来提取电话号码?
    正则表达式,是描述匹配模式(pattern)的文本字符串,用于搜索和匹配字符串集。简单来说,就是可以通过从一堆字符串里,搜索提取自己想要的内容。Excel网络函数库帮助使用者们在Ex......
  • excel、word、PPT中插入PDF文件不显示图标问题
    插入PDF对象,不显示正确的PDF图标临时解决办法:手动修改对象图标具体位置,可在C:\Windows\Installer目录下搜索PDFFile_8.ico尝试如下操作:手动复制生成C:\Windows\SysW......
  • Java 导出word、pdf、excel的echart图形
    引用文章:https://www.codenong.com/cs109245248/利用jfreechart依赖导出点击查看代码<dependency><groupId>org.jfree</groupId><artifactId>jfreechart<......
  • Web前端学习笔记2——表格
    表格的基本语法以及标签表格示例1:table标签就是表格的标签,用来建立一个表格tr标签表示行,一个<tr></tr>标签就是一行td标签表示数据单元格的内容,可以理解为列th标签为......
  • 循环某类型列表,组装类型列表中所有下级名称,展示表格使用
    /***获取数据信息**@paramselectBOS原类型列表*@parami类型下标(从0开始)*@paramname类型拼接名称(拼接后的每行......
  • word vba 操作表格, 设置表格中的段落
    一、表格中的段落设置Sub表格中的段落设置()DimpAsParagraphForEachpInActiveDocument.ParagraphsIfp.Range.Information(wdWithInTable)Then'针对......
  • 通过数据库两种方式修改WordPress密码
    这里介绍两种方式修改WordPress密码方式:方法一、mysql修改后台密码1)登上数据库,使用的语句是:mysql-uroot-p 2)输入语句查看所有数据库名:showdata......
  • SiteFactory支持Word图片粘贴
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java"......
  • New password: BAD PASSWORD: The password contains the user name in some form
    有些时候服务器规定了用户的密码中不允许包含用户名,例如test用户你不能使用类似abtest123这种密码,但是我就是想要这么设置,可以使用chpasswd设置,我不确定是否在所有情况下都......
  • SiteFactory支持Word图片上传
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑......