我正在尝试在我的word文档中替换100个测试ID。我从这个网站找到了下面提到的宏并使用过。我面临的问题是,这个逻辑并没有取代完全匹配的值。当我尝试将测试ID AUTO_1更改为AUTO_A时,它还将测试ID AUTO_10更改为AUTO_A0,将AUTO_11更改为AUTO_A1。
Sub test1()
Dim pathh As String
Dim pathhi As String
Dim oCell As Integer
Dim from_text As String, to_text As String
Dim WA As Object
pathh = "C:\1.doc"
Set WA = CreateObject("Word.Application")
WA.Documents.Open (pathh)
WA.Visible = True
For oCell = 1 To 100
from_text = Sheet1.Range("A" + CStr(oCell)).Value
to_text = Sheet1.Range("B" + CStr(oCell)).Value
With WA
.Activate
With .Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = from_text
.Replacement.Text = to_text
.Execute Replace:=wdReplaceAll
End With
End With
Next
End Sub
答案 0 :(得分:0)
尝试将from_text更改为:
from_text = Sheet1.Range("A" + CStr(oCell)).Value & " "
您也可以反向循环,以便首先替换较大的数字:
For ocell = 100 to 1 Step -1
...
Next ocell