5,172
社区成员




Sub 列表替换()
Dim Path$, iRow%, i%
Dim wd As Document
Path = ActiveDocument.Path
Set wb = GetObject(Path & "\13.xlsx") '需要把替换的excel放到word同一个目录下
With wb.sheets(1)
iRow = .Range("A1").CurrentRegion.Rows.Count
For i = 2 To iRow
Set wd = ActiveDocument
'查找替换
Rep wd, .Cells(i, 1).Value, .Cells(i, 2).Value
Next i
End With
wb.Close
End Sub
'替换
Sub Rep(wordD, FindCode As String, ReplaceCode As String)
With wordD.Range
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = FindCode
.Replacement.Text = ReplaceCode
.MatchWildcards = True
End With
.Find.Execute Replace:=wdReplaceAll
End With
End Sub