|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
参考收藏的代码:
Sub 自动添加字符方框()
Const myEQ As String = "EQ \X("
Dim myString As String
Dim strTemp As String
Dim bytLenth As Byte
Dim i As Byte
Dim oChar As String * 1
Dim LastRange As Range
Dim NewField As Field
myString = InputBox("请输入您的目标字符!")
bytLenth = Len(myString)
If bytLenth = 0 Then Exit Sub
Application.ScreenUpdating = False '关闭屏幕更新
If Selection.Type <> wdSelectionIP Then Selection.Delete '总是删除所选内容
With ActiveDocument
For i = 1 To bytLenth '建立一个循环
oChar = Mid(myString, i, 1)
Set LastRange = Selection.Range
Set NewField = .Fields.Add(LastRange, wdFieldEmpty, myEQ & oChar & ")", False)
strTemp = NewField.Code.Text '获得域代码
strTemp = VBA.RTrim(strTemp) '去除域代码的右侧空格
NewField.Code.Text = strTemp '重写域代码
NewField.Code.Font.Name = "Arial" '设置域代码的字体
NewField.Code.Font.Size = 10 '字体大小
.Fields.Update '更新域
Next
End With
Application.ScreenUpdating = True '恢复屏幕更新
End Sub |
|