当前位置: 首页 > 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.rkmt.cn/news/10757.html

相关文章:

  • 深入解析:find_code 插件 react_vite
  • SAP BAPI_PR_CREATE 创建采购申请(含自定义字段)
  • NCCL论文阅读
  • 皇牌空战7豪华版DLC补丁
  • BeanUtils中的copyProperties方法使用和分析
  • WoTerm、WindTerm及putty的性能测试对比
  • Python - csv.writer()
  • BM25 关键词检索算法
  • 55.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--实现手机邮箱登录 - 实践
  • 详细介绍:Xilinx系列FPGA实现12G-SDI音视频编解码,支持4K60帧分辨率,提供2套工程源码和技术支持
  • 使用 VMware Workstation 安装 CentOS-7 虚拟机
  • uv Python安装镜像加速
  • AT_arc167_c [ARC167C] MST on Line++
  • CentOS操作系统
  • window系统下使用二进制包安装MySQL数据库
  • 在Vona ORM中实现多数据库/多数据源
  • sql over()函数使用
  • 小柏实战学习Liunx(图文教程三十二)
  • VPX处理板设计原理图:9-基于DSP TMS320C6678+FPGA XC7V690T的6U VPX信号处理卡 C6678板卡, XC7VX690T板卡, VPX处理板
  • VitePress 添加友链界面
  • 洛谷题单指南-进阶数论-P1495 【模板】中国剩余定理(CRT)/ 曹冲养猪
  • 发现5个宝藏文件摆渡系统 2025年企业首选的摆渡方案是这个!
  • BilldDesk:基于Vue3+WebRTC+Nodejs+Electron的开源远程桌面控制 - 详解
  • 查看linux部署网站的TLS版本号
  • 按照DDD的方式写的一个.net有关Web项目框架
  • 【习题答案】《深入理解计算机系统(原书第三版)》
  • 软件体系结构——负载均衡 - 指南
  • Qwen3-Max 2025年完整发布解析:阿里巴巴最强AI模型深度评测
  • css-伪元素清除浮动
  • 在K8S中,Deployment⽀持扩容吗?它与HPA有什么区别?