如何在word里有规律的替换数字

2024-11-23 18:37:04
推荐回答(1个)
回答1:

Sub Macro1()
Dim nStr As String, Str1 As String, Str2 As String
Dim I As Long, M As Long, N As Long, S As Long

Str1 = "dddd" '待替换的字符
N = Len(Str1)
nStr = ActiveDocument.Range.Text
Do
S = InStr(S + 1, nStr, Str1)
If S = 0 Then Exit Do
I = I + 1
Str2 = I
M = 4 - Len(Str2)
If M > 0 Then Str2 = String(M, "0") & Str2
nStr = Left(nStr, S - 1) & Str2 & Mid(nStr, S + N)
S = S + N - 1
Loop
ActiveDocument.Range.Text = nStr
End Sub