ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 批量修改word文本框等自选图形格式

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-5-27 10:18 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

word中的文本框等自选图形对象,一般包括三部分:画布(里面有文本框、连接符等图形)、组合图形(里面有文本框、连接符等图形)及非画布和非组合图形。画布和组合图形的好处是可以作为盛放多个文本框等的“容器,方便作为一个整体复制到新的文档中。

统计图形个数时,1个画布或1个组合图形都反馈只有1shape图形而不统计下属的子图形个数,下属的子图形分别用CanvasItems集合和GroupItems集合表示。

以下是一个通用的操作画布、组合图形和非画布和非组合图形代码,可以根据实际情况修改。
=========================================================================================


Sub d1()
    Rem 批量修改word文本框等自选图形格式
    Application.ScreenUpdating = False
    On Error Resume Next
    Dim arr()
    k = 0
    For Each oShp In ActiveDocument.Shapes
        ReDim Preserve arr(k)
        arr(k) = oShp.Name
        k = k + 1
    Next
    For j = 0 To UBound(arr)
        sName = arr(j)
        Set oShp = ActiveDocument.Shapes(sName)
        '=====================================
        If oShp.Type = msoCanvas Then '操作画布(如果画布内还有组合图形,则把该组合图形作为画布内的一个整体作处理)
            For i = 1 To oShp.CanvasItems.Count
                If oShp.CanvasItems(i).Type = msoLine Or oShp.CanvasItems(i).Name Like "*Connector*" Or oShp.CanvasItems(i).Name Like "*Freeform*" Then '排除各种连接符、直线等 (如果对直线起作用,如设置直线的颜色等,需修改代码)
                Else
                    Debug.Print oShp.CanvasItems(i).Name
                    With oShp.CanvasItems(i).TextFrame.TextRange
                        '.Text = "1"
                        '.Font.Name = "宋体"
                        .Font.Color = vbBlack
                        '.Font.Size = 8
                        '.ListFormat.ConvertNumbersToText '转手动编号
                        '.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
                        '.ParagraphFormat.DisableLineHeightGrid = -1 '取消对齐到文档网格
                        With .ParagraphFormat
                            '.CharacterUnitFirstLineIndent = 0
                            '.FirstLineIndent = 0
                            '.CharacterUnitLeftIndent = 0
                            '.LeftIndent = 0
                            '.CharacterUnitRightIndent = 0
                            '.RightIndent = 0
                            '.Alignment = wdAlignParagraphCenter
                        End With
                    End With
                    With oShp.CanvasItems(i).TextFrame
                        '.MarginTop = CentimetersToPoints(0)
                        '.MarginBottom = CentimetersToPoints(0)
                        '.MarginLeft = CentimetersToPoints(0)
                        '.MarginRight = CentimetersToPoints(0)
                        .VerticalAnchor = msoAnchorMiddle '文本框内文本垂直中部对齐
                    End With
                    'oShp.CanvasItems(i).Fill.ForeColor = vbYellow '文本框底纹
                    oShp.CanvasItems(i).Fill.Visible = False '文本框无底纹颜色
                    oShp.CanvasItems(i).Line.ForeColor = vbBlack '文本框轮廓颜色、连接符颜色
                    oShp.CanvasItems(i).Line.Weight = 0.5 '文本框轮廓粗细、连接符粗细
                    'oShp.CanvasItems(i).Height = CentimetersToPoints(1.1) '文本框高度
                    'oShp.CanvasItems(i).Width = CentimetersToPoints(2.5) '文本框宽度
                End If
            Next
        End If
        '=====================================
        If oShp.Type = msoGroup Then '操作组合图形(也适用于组合图形下的子组合图形)
            For i = 1 To oShp.GroupItems.Count
                If oShp.GroupItems(i).Type = msoLine Or oShp.GroupItems(i).Name Like "*Connector*" Or oShp.GroupItems(i).Name Like "*Freeform*" Then '排除各种连接符、直线等 (如果对直线起作用,如设置直线的颜色等,需修改代码)
                Else
                    With oShp.GroupItems(i).TextFrame.TextRange
                        '.Text = "1"
                        '.Font.Name = "宋体"
                        .Font.Color = vbBlack
                        '.Font.Size = 8
                        '.ListFormat.ConvertNumbersToText '转手动编号
                        '.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
                        '.ParagraphFormat.DisableLineHeightGrid = -1 '取消对齐到文档网格
                        With .ParagraphFormat
                            '.CharacterUnitFirstLineIndent = 0
                            '.FirstLineIndent = 0
                            '.CharacterUnitLeftIndent = 0
                            '.LeftIndent = 0
                            '.CharacterUnitRightIndent = 0
                            '.RightIndent = 0
                            '.Alignment = wdAlignParagraphCenter
                        End With
                    End With
                    With oShp.GroupItems(i).TextFrame
                        '.MarginTop = CentimetersToPoints(0)
                        '.MarginBottom = CentimetersToPoints(0)
                        '.MarginLeft = CentimetersToPoints(0)
                        '.MarginRight = CentimetersToPoints(0)
                        .VerticalAnchor = msoAnchorMiddle '文本框内文本垂直中部对齐
                    End With
                    'oShp.GroupItems(i).Fill.ForeColor = vbYellow '文本框底纹
                    oShp.GroupItems(i).Fill.Visible = False '文本框无底纹颜色
                    oShp.GroupItems(i).Line.ForeColor = vbBlack '文本框轮廓颜色、连接符颜色
                    oShp.GroupItems(i).Line.Weight = 0.5 '文本框轮廓粗细、连接符粗细
                    'oShp.GroupItems(i).Height = CentimetersToPoints(1.1) '文本框高度
                    'oShp.GroupItems(i).Width = CentimetersToPoints(2.5) '文本框宽度
                End If
            Next
        End If
        '=====================================
        Rem 注意个别情况下会对图片或其他图形对象误操作
        If Not oShp.Type = msoGroup And Not oShp.Type = msoCanvas Then '操作非画布和非组合图形
            If oShp.Type = msoLine Or oShp.Name Like "*Connector*" Or oShp.Name Like "*Freeform*" Then '排除各种连接符、直线等(如果对直线起作用,如设置直线的颜色等,需修改代码)
            Else
                'If oShp.GroupItems.Count = 0 And oShp.CanvasItems.Count = 0 Then
                With oShp.TextFrame.TextRange
                    '.Text = "1"
                    '.Font.Name = "宋体"
                    .Font.Color = vbBlack
                    '.Font.Size = 8
                    '.ListFormat.ConvertNumbersToText '转手动编号
                    '.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
                    '.ParagraphFormat.DisableLineHeightGrid = -1 '取消对齐到文档网格
                    With .ParagraphFormat
                        '.CharacterUnitFirstLineIndent = 0
                        '.FirstLineIndent = 0
                        '.CharacterUnitLeftIndent = 0
                        '.LeftIndent = 0
                        '.CharacterUnitRightIndent = 0
                        '.RightIndent = 0
                        '.Alignment = wdAlignParagraphCenter
                    End With
                End With
                With oShp.TextFrame
                    '.MarginTop = CentimetersToPoints(0)
                    '.MarginBottom = CentimetersToPoints(0)
                    '.MarginLeft = CentimetersToPoints(0)
                    '.MarginRight = CentimetersToPoints(0)
                    .VerticalAnchor = msoAnchorMiddle '文本框内文本垂直中部对齐
                End With
                'oShp.Fill.ForeColor = vbYellow '文本框底纹
                oShp.Fill.Visible = False '文本框无底纹颜色
                oShp.Line.ForeColor = vbBlack '文本框轮廓颜色、连接符颜色
                oShp.Line.Weight = 0.5 '文本框轮廓粗细、连接符粗细
                'oShp.WrapFormat.AllowOverlap = False '禁止重叠
                'oShp.Height = CentimetersToPoints(1.1) '文本框高度
                'oShp.Width = CentimetersToPoints(2.5) '文本框宽度
                'End If
            End If
        End If
    Next
    '=====================================
    Application.ScreenUpdating = True
End Sub




TA的精华主题

TA的得分主题

 楼主| 发表于 2020-5-27 10:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
这是一个删除文本框内回车符的示例代码,也可以改成其他的替换方式。

===================================
Sub d5()
Rem word文本框内文字查找替换
    Application.ScreenUpdating = False
    On Error Resume Next
    '也可用于其他矩形等其他含文字图形的替换
    Dim arr()
    k = 0
    For Each oShp In ActiveDocument.Shapes
        ReDim Preserve arr(k)
        arr(k) = oShp.Name
        k = k + 1
    Next
   
    For j = 0 To UBound(arr)
        sName = arr(j)
        Set oShp = ActiveDocument.Shapes(sName)
        Rem 操作画布
        If oShp.Type = msoCanvas Then
            For n = 1 To oShp.CanvasItems.Count
                oShp.CanvasItems(n).TextFrame.TextRange.Find.Execute findtext:="^13", replacewith:="", Replace:=wdReplaceAll, MatchWildcards:=True
            Next
        End If
        Rem 操作组合图形
        If oShp.Type = msoGroup Then
            For n = 1 To oShp.GroupItems.Count
                oShp.GroupItems(n).TextFrame.TextRange.Find.Execute findtext:="^13", replacewith:="", Replace:=wdReplaceAll, MatchWildcards:=True
            Next
        End If
        Rem 操作非画布和非组合图形
        If Not oShp.Type = msoGroup And Not oShp.Type = msoCanvas Then
                oShp.TextFrame.TextRange.Find.Execute findtext:="^13", replacewith:="", Replace:=wdReplaceAll, MatchWildcards:=True
        End If
    Next
    Application.ScreenUpdating = True
End Sub


TA的精华主题

TA的得分主题

发表于 2020-7-29 16:21 | 显示全部楼层
怎么判断任意多边形内是否含有文字,
If oShp.GroupItems(i).Type = msoFreeform Then '任意多边形是否含有文字
                       If oShp.GroupItems(i).TextFrame.TextRange = "" Then
                      oShp.GroupItems(i).Fill.Visible = False '不显示底纹颜色
                       Else
                       oShp.GroupItems(i).Fill.ForeColor = RGB(100, 100, 100) '文本框底纹
                       oShp.GroupItems(i).Fill.Visible = True '显示底纹颜色
                       oShp.GroupItems(i).TextFrame.TextRange.Style = ActiveDocument.Styles("五中") '字体样式
                      End If
                    End If
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-26 22:31 , Processed in 0.032890 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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