当前位置: 首页 > news >正文

Word-通过宏格式化文档中的表格和图片

1、打开Word文档,找到”视图“,然后点击”宏“。

image

 

2、创建宏脚本。

Sub 表格格式化()On Error Resume Next   ' 忽略合并单元格等错误Application.ScreenUpdating = FalseDim tbl As Table, col As ColumnDim counter As Integer: counter = 1Dim response As VbMsgBoxResultDim title As String'' 遍历文档中所有表格For Each tbl In ActiveDocument.Tables'' --- 核心设置 ---'' 1. 表格宽度固定为14.63厘米tbl.PreferredWidthType = wdPreferredWidthPoints      ' 宽度类型tbl.PreferredWidth = CentimetersToPoints(14.63)      ' 表格宽度tbl.Rows.Alignment = wdAlignRowCenter                ' 居中显示tbl.AllowAutoFit = False'' 设置表格宽度为 100%(相对于页面宽度)' tbl.PreferredWidthType = wdPreferredWidthPercent' tbl.PreferredWidth = 100' tbl.Rows.Alignment = wdAlignRowCenter             ' 居中显示' tbl.AllowAutoFit = False' 设置表格所在段落的行距为单倍行距tbl.Range.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle' 设置表格前后段落' Set para = tbl.Range.Paragraphs(1)' With para.Format'     .LineSpacingRule = wdLineSpaceSingle'    .SpaceBefore = 0'    .SpaceAfter = 0' End With' 设置表格行高和居中对齐With tbl.Rows.Height = CentimetersToPoints(1).Rows.HeightRule = wdRowHeightExactly' .Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter   ' 垂直居中' .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter    ' 水平居中End With'' 2. 统一边框设置With tbl.Borders.Enable = True.InsideLineStyle = wdLineStyleSingle.InsideLineWidth = wdLineWidth50pt  ' 内部细线End With'' 3. 首行格式:灰底黑体10号加粗居中Set headerRow = tbl.Rows(1)  ' 获取第一行(标题行)With headerRow.Range.Font.NameFarEast = "宋体"  ' 中文字体.Font.Size = 10.5           ' 字体大小.Font.Bold = True           ' 是否加粗显示.Font.Color = RGB(0, 0, 0)  ' 字体颜色.Shading.BackgroundPatternColor = RGB(242, 242, 242)  ' 浅灰底纹.ParagraphFormat.Alignment = wdAlignParagraphCenter   ' 水平居中.Cells.VerticalAlignment = wdCellAlignVerticalCenter  ' 垂直居中End With'' 4. 设置其他行格式If tbl.Rows.Count >= 2 ThenFor r = 2 To tbl.Rows.CountWith tbl.Rows(r).Range.Font.NameFarEast = "宋体"     ' 中文字体.Font.Color = RGB(0, 0, 0)     ' 黑色字体.Font.Size = 10.5              ' 字体大小.Shading.BackgroundPatternColor = wdColorAutomatic  ' 清除底纹.ParagraphFormat.Alignment = wdAlignParagraphCenter   ' 水平居中.Cells.VerticalAlignment = wdCellAlignVerticalCenter  ' 垂直居中End WithNext rEnd If'' --- 每10个表格提示一次 ---If counter Mod 10 = 0 Thenresponse = MsgBox("已处理第 " & counter & " 个表格" & vbNewLine & _"点击【确定】继续,【取消】中止", _vbOKCancel + vbInformation, "批量进度提示")If response = vbCancel ThenApplication.ScreenUpdating = TrueMsgBox "操作已中止!共完成 " & (counter - 1) & " 个表格", vbExclamationExit ForEnd IfEnd Ifcounter = counter + 1Next tbl'' 收尾处理Application.ScreenUpdating = TrueIf response <> vbCancel ThenMsgBox "操作完成!共处理 " & (counter - 1) & " 个表格", vbInformationEnd IfEnd Sub'' 厘米转磅函数(1厘米=28.35磅)
Function CentimetersToPoints(ByVal cm As Single) As SingleCentimetersToPoints = cm * 28.35
End Function'' 设置图片居中显示,大小调整为页面宽度
Sub 图片格式化()' 声明变量Dim shp As ShapeDim ilshp As InlineShapeDim pageWidth As SingleDim leftMargin As SingleDim rightMargin As SingleDim usableWidth As Single' 关闭屏幕更新以提高宏运行速度Application.ScreenUpdating = False' 设置错误处理,跳过无法处理的图片On Error Resume Next' 计算页面可用宽度(点数)With ActiveDocument.PageSetuppageWidth = .pageWidthleftMargin = .leftMarginrightMargin = .rightMarginEnd With' 计算可用宽度 = 页面宽度 - 左边距 - 右边距usableWidth = pageWidth - leftMargin - rightMargin' 第一部分:处理嵌入型图片For Each ilshp In ActiveDocument.InlineShapesIf ilshp.Type = wdInlineShapePicture Or ilshp.Type = wdInlineShapeLinkedPicture Then' 设置图片宽度为页面可用宽度ilshp.Width = usableWidthEnd IfNext ilshp' 第二部分:处理浮动型图片For Each shp In ActiveDocument.ShapesIf shp.Type = msoPicture Or shp.Type = msoLinkedPicture Then' 锁定纵横比,设置宽度为页面可用宽度shp.LockAspectRatio = msoTrueshp.Width = usableWidthEnd IfNext shp' 完成提示MsgBox "已完成!所有图片已设置为页面宽度。" & vbCrLf & _"页面可用宽度: " & Format(usableWidth, "0.00") & " 点", vbInformation' 重新开启屏幕更新Application.ScreenUpdating = TrueEnd Sub

 

3、运行宏脚本,选中"表格格式化",点击”运行“ 按钮,即可格式化表格。

image

 执行完成后,弹出对话框。

image

 

4、运行宏脚本,选中"图片格式化",点击”运行“ 按钮,即可格式化图片。

image

  执行完成后,弹出对话框。

image

 

http://www.sczhlp.com/news/131613/

相关文章:

  • 网站建设上机课网站建设租房网模块
  • 网站建设行业新闻动态上海巨型网站建设
  • 怎样用模板做网站建网站的资料
  • 网站建设与运维预算上海办公室设计装修公司
  • 建站模板源码seo网站内容
  • 建网站要会什么wordpress页面连接
  • 推荐6个国外自媒体平台对网站做综合搜索引擎优化分析
  • 做网站的叫什么信息流优化师没经验可以做吗
  • 指数 网站权重浙江省建设工程造价协会网站
  • 反向代理 traefik - 健康检查
  • 一些想法 - CelestialZ
  • 使用 Ansible 批量安装 Docker
  • 编程规范---日志规范
  • 深入解析:find_code 插件 react_vite
  • 湖北人工智能建站系统软件wordpress文章的分享代码
  • 卡片式网站wordpress4.5 ueditor 1.4.3
  • 别人给公司做的网站字体侵权吗asp做的手机网站
  • 自己做个网站用什么软件好企业宣传及介绍ppt
  • 各种颜色做网站给人的心里暗示摄影摄像网站建设
  • 永州网站建设网站seo搜索如何优化
  • 广西建设网站首页哪个网站音乐做的最好的
  • 常德建设局官方网站设计头条官网
  • 二十一、DevOps:从零建设基于K8s的DevOps平台(二)
  • 中电金信:从“通用”到“专用”:加速实现金融行业生成式AI应用的必由之路
  • 自动构建高质量测试集
  • SAP BAPI_PR_CREATE 创建采购申请(含自定义字段)
  • NCCL论文阅读
  • 网站建设 律师wordpress小说站模板
  • 建怎么网站比较赚钱企业网站备案要多久
  • 河南省旅游网站建设泉州建站哪些公司