ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2521|回复: 14

WORD调整表格样式

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-7-23 21:37 | 显示全部楼层 |阅读模式
经常要用到word,且文档里很多表格,请问怎么用VBA一次性修改所有表格的格式,具体要求:
1、表格属性:
(1)对齐方式:居中
(2)文字环绕:无
(3)指定高度:0.8厘米,行高值是:最小值
2、表格内文字要求:
(1)中文字体:宋体 五号,英文字体:New Time Roman 5号;
(2)行距:固定值 20磅;
(3)对齐方式:水平居中,垂直居中;
3、表格自动调整方式:先根据内容自动调整表格,然后根据窗口自动调整表格。
要求比较多,奈何自己不懂VBA,恳请大神帮忙给段代码,感激不尽!


TA的精华主题

TA的得分主题

发表于 2018-7-24 00:08 | 显示全部楼层
楼主,表格固定值20磅,文字将不再居中!所以,恕难从命,表格仍为单倍行距,这样文字上下左右才居中。请试试如下代码(并非完美,勉强先用用吧!):
  1. Sub test表格样式()
  2.     Dim t As Table, c As Cell, i As Paragraph, j&, k&, x&, y&, z&, e&
  3.     For Each t In ActiveDocument.Tables
  4.         With t
  5.             With .Range '探测表格是否规则(e=1=规则,e=0=不规则)
  6.                 .Find.Execute "^13", , , 0, , , , , , "^p", 2
  7.                 .Find.Execute "^11", , , 0, , , , , , "^p", 2
  8.                 x = .Information(wdEndOfRangeRowNumber)
  9.                 y = .Information(wdEndOfRangeColumnNumber)
  10.                 z = .Cells.Count
  11.             End With
  12.             If x <> 1 Then
  13.                 If z = x * y Then
  14.                     For k = 1 To y
  15.                         For j = 1 To x - 1
  16.                             If .Cell(j + 1, k).Width = .Cell(j, k).Width Then e = 1 Else e = 0
  17.                             If e = 0 Then Exit For
  18.                         Next j
  19.                         If e = 0 Then Exit For
  20.                     Next k
  21.                 Else
  22.                     e = 0
  23.                 End If
  24.             Else
  25.                 e = 1
  26.             End If
  27.             With .Rows
  28.                 .WrapAroundText = False
  29.                 .Alignment = wdAlignRowLeft
  30.                 .LeftIndent = CentimetersToPoints(0)
  31.                 .HeightRule = wdRowHeightAtLeast
  32.                 .Height = CentimetersToPoints(0.8)
  33.             End With
  34.             .LeftPadding = CentimetersToPoints(0.19)
  35.             .RightPadding = CentimetersToPoints(0.19)
  36.             .Select
  37.             Selection.MoveDown 4, 1, 1
  38.             CommandBars.FindControl(ID:=122).Execute
  39.             CommandBars.FindControl(ID:=123).Execute
  40.             .Select
  41.             Selection.ClearFormatting
  42.             With .Range
  43.                 With .Font
  44.                     .Kerning = 0
  45.                     .DisableCharacterSpaceGrid = True
  46.                 End With
  47.                 With .ParagraphFormat
  48.                     .CharacterUnitFirstLineIndent = 0
  49.                     .FirstLineIndent = CentimetersToPoints(0)
  50.                     .LineSpacingRule = wdLineSpaceSingle
  51.                     .Alignment = wdAlignParagraphCenter
  52.                     .AutoAdjustRightIndent = False
  53.                     .DisableLineHeightGrid = True
  54. '                    .LineSpacingRule = wdLineSpaceExactly'固定值20磅
  55. '                    .LineSpacing = 20
  56.                 End With
  57.                 .Cells.VerticalAlignment = wdCellAlignVerticalCenter
  58.             End With
  59.             .AutoFitBehavior (wdAutoFitContent) '根据内容调整表格
  60.             .AutoFitBehavior (wdAutoFitContent)
  61.             .Select
  62.             .AutoFitBehavior (wdAutoFitWindow) '根据窗口调整表格
  63.             .AutoFitBehavior (wdAutoFitWindow)
  64.             '规则表格表头加粗(表头即表格第一行)
  65.             If e = 1 Then
  66.                 If Len(.Cell(2, 1).Range) > 2 Then
  67.                     With .Rows(1).Range.Font
  68.                         .Name = "黑体"
  69.                         .Name = "Times New Roman"
  70.                         .Bold = True
  71.                         .Color = wdColorRed
  72.                     End With
  73.                 End If
  74.             End If
  75.             '删除单元格内空行
  76.             For Each c In .Range.Cells
  77.                 For Each i In c.Range.Paragraphs
  78.                     If Asc(i.Range) = 13 And Len(i.Range) = 1 Then i.Range.Delete
  79.                 Next
  80.                 With c.Range.Paragraphs
  81.                     If .Count > 1 And Len(.Last.Range) = 2 Then
  82.                         .Last.Previous.Range.Characters.Last.Delete
  83.                     End If
  84.                 End With
  85.             Next
  86.         End With
  87.     Next
  88. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-7-24 08:40 | 显示全部楼层
  1. Sub 表格排版()
  2.     Dim TablesCount%, doc As Document
  3.     Set doc = ActiveDocument
  4.     TablesCount = doc.Tables.Count
  5.     If TablesCount = 0 Then Exit Sub '无表格退出
  6.    
  7.     Dim myRng As Range, myTable As table
  8.     For Each myTable In doc.Tables
  9.         Set myRng = myTable.Range
  10.         With myRng                                '表格文字排版
  11.             .Font.Size = 10.5                   '五号字体
  12.             .Font.Name = "宋体"
  13.             With .ParagraphFormat
  14.                 .LineSpacingRule = wdLineSpaceExactly '行距-固定值
  15.                 .LineSpacing = 20
  16.                 .Alignment = wdAlignParagraphCenter
  17.                 .CharacterUnitFirstLineIndent = 0
  18.                 .FirstLineIndent = 0
  19.             End With
  20.         End With
  21.         With myRng
  22.             .Rows.Alignment = wdAlignRowCenter
  23.             .Rows.AllowBreakAcrossPages = False '允许断页
  24.             .Rows.WrapAroundText = False        '无环绕
  25.             .Rows.HeightRule = wdRowHeightExactly '行高设为最小值
  26.             .Rows.Height = CentimetersToPoints(0.8) '行高
  27.             .ParagraphFormat.Alignment = wdAlignParagraphCenter '水平居中
  28.             .Cells.VerticalAlignment = wdCellAlignVerticalCenter  '垂直居中
  29.             .Columns.PreferredWidthType = wdPreferredWidthAuto
  30.             '.Columns.PreferredWidth = 0
  31.             With .Tables(1)
  32. '                .TopPadding = CentimetersToPoints(0)
  33. '                .BottomPadding = CentimetersToPoints(0)
  34. '                .LeftPadding = CentimetersToPoints(0)
  35. '                .RightPadding = CentimetersToPoints(0)
  36. '                .Spacing = CentimetersToPoints(0)
  37. '                .AllowPageBreaks = False
  38. '                .AllowAutoFit = True
  39.                 .AutoFitBehavior (wdAutoFitContent)  '根据内容调整表格
  40.                 .AutoFitBehavior (wdAutoFitWindow)   '根据窗口调整表格
  41.             End With
  42.         End With
  43.     Next
  44. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2018-7-24 11:35 | 显示全部楼层
ming朋友:终于出手了!厉害!我要好好学学你的代码。——楼主,请用 ming朋友的代码吧,他是高手,我是低手。

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-7-24 12:44 | 显示全部楼层

非常感谢,很好用。发现2个小问题:
(1)宏设置出来的表格行高是固定值,不是最小值,百度了下,把wdRowHeightExactly改成wdRowHeightAtLeast就好了;
(2)所有字体都是宋体,没有把英文改为New Time Roman,已经百度解决了。
再次感谢大神!

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-7-24 12:46 | 显示全部楼层
413191246se 发表于 2018-7-24 11:35
ming朋友:终于出手了!厉害!我要好好学学你的代码。——楼主,请用 ming朋友的代码吧,他是高手,我是低 ...

采用了ming0018的代码。
也谢谢你的帮忙写的代码,感谢!

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-7-24 12:49 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

非常感谢,很好用,虽然有2个小问题(没有变更英文字体,表格行高是固定值而非最小值),不过我都百度解决了。

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-7-24 13:04 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

您好!再请教下,使用过程中发现,我文档中有些表格格式要求不是这样的,而这个宏是将文档内所有的表格都修改成这样。如果我想只修改我选定了的表格的格式,这个宏应该怎么修改?感谢。

TA的精华主题

TA的得分主题

发表于 2018-7-24 14:34 来自手机 | 显示全部楼层
把不改的再复制粘贴回来。

TA的精华主题

TA的得分主题

发表于 2018-7-24 14:35 | 显示全部楼层
Sky11235 发表于 2018-7-24 12:49
非常感谢,很好用,虽然有2个小问题(没有变更英文字体,表格行高是固定值而非最小值),不过我都百度解 ...

随便写的,是的,表格行高我设成固定值了,英文字体很简单了,既然你已经解决了,说明你的基本功还是有的,有发展的空间!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-8 22:08 , Processed in 0.050372 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表