繁体   English   中英

从MS Excel中的列表中的MS Word中多个查找和替换

[英]Multiple find and replace in MS Word from a list in MS Excel

嗨,我真的希望您能为我提供帮助,因为我已经尝试了一段时间,但运气不佳。

我在Excel中有一个列表,例如文件1(例如A1-B10,两列单词-A列中的单词被B列中的单词替换)。

我有一个文档,比如说文件2,我想运行excel文件中的单词,以便将excel文件A列中任何单词的每次出现都替换为B列中的相应单词。

非常感谢您能给我提供的任何帮助,非常感谢。

如果我理解正确,那么您想用Excel文件中列出的Word替换Word文档中的Word。 如果是这样,此宏应该可以解决问题(适用于MS Word的宏):

Function findAndReplace()
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object

Dim i As Integer, j As Integer
Dim lastRow As Integer

'Set Objects
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("PATH TO EXCEL FILE") 'Replace String with path to Excel File
Set xlWS = xlWB.Worksheets("Name of Worksheet") 'Replace String with your Worksheet Name

'get last row of excel file
lastRow = xlWS.UsedRange.SpecialCells(xlCellTypeLastCell).Row

'loop through all words in Word Document
For i = 1 To ThisDocument.Words.Count - 1 Step 1

    'Loop through cells in Excel File
    For j = 1 To lastRow Step 1

        'Replace Word value in Column B of Excel File
        ThisDocument.Words(i) = Replace(ThisDocument.Words(i), xlWS.Cells(j, 1).Value, xlWS.Cells(j, 2).Value)
    Next j
Next i

'Close Excel and Cleanup
Set xlWS = Nothing
xlWB.Close True
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function

如果我理解您的意思不对,请向我们详细说明您要尝试执行的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM