使用VBA根据文本样式(颜色/字体/字号)进行定位并追加内容

需求说明

一个doc文档中有大量的特殊样式文本,使用相同的样式黑体,三号(16),红色(RGB:255 0 0),现在需要将这些特殊样式的文本都是使用#符号包围起来,如下图所示:
在这里插入图片描述

VBA代码实现

Sub batchEdit()
'
' 批量根据样式修改文本内容

'
With Selection.Find.Font
    .NameFarEast = "黑体"
    .Size = 16
    .Color = RGB(255, 0, 0)
End With

With Selection.Find
.Text = ""  ' 不指定要查找的文本内容,只按照样式搜索
.Replacement.Text = "#^&#"  ' 使用^&表示原文本内容
.Format = True  ' 设置按照样式进行搜索
End With

Selection.Find.Execute Replace:=wdReplaceAll

End Sub
相关推荐
自动生成VBA窗体菜单 '*************************** '* 菜单类 * '*************************** Option Explicit Private WithEvents MenuBar_MenuItem As MSForms.Label '菜单项 Private WithEvents WorkForm As MSForms.UserForm '工作窗口 Private WithEvents MenuBar As MSForms.Image '菜单栏 Private BackMenu_BackGroud As MSForms.Image '菜单背景图片 Private BackMenu_Caption As MSForms.Label '菜单标题标签 Private Const DISTANCE As Integer = 5 '菜单与左边框距离 Private Const MENUTOP As Integer = 2 '菜单项顶点Y轴位置 Private Const MENUHEIGHT As Integer = 14 '菜单项高度 Private intIndex As Integer '索引变量 Private sAction As String '宏名称变量 Private Property Let Index(N As Byte) '指定索引属性 intIndex = N End Property Private Property Get Index() As Byte '获得陇望蜀索引属性 Index = intIndex End Property Private Property Let OnAction(sAct As String) '行为属性 sAction = sAct End Property Private Property Get OnAction() As String OnAction = sAction End Property Public Sub AddMenu(wform As MSForms.UserForm, sCaption As String, sAction As String, Optional Acc As String = vbNullString) Dim MenuLeft As Single, MenuWidth As Single '由两个标签和一个图形控件组成一个主菜单项 MenuCount = MenuCount + 1 '主菜单项总数加1 Index = MenuCount '设置索引 Set WorkForm = wform With WorkForm Set MenuBar = .FormMenuBar Set BackMenu_Caption = .Controls.Add("forms.label.1") '添加一个标签,显示菜单标题 With BackMenu_Caption .Accelerator = Acc .AutoSize = True .BackStyle = fmBackStyleTransparent .Caption = sCaption .Font = "宋体" .Font.Size = 9 .Name = "BackMenu_Caption" & MenuCount .TextAlign = fmTextAlignCenter .Top = MENUTOP + 3 .WordWrap = False .Visible = True End With If MenuCount = 1 Then MenuLeft = DISTANCE Else With .Controls("BackMenu_Caption" & MenuCount - 1) MenuLeft = .Left + .Width End With End If MenuWidth = BackMenu_Caption.Width + 10
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页