VBA搜索替换Excel中的Word文档中的文本

时间:2018-11-13 18:54:52

标签: excel vba ms-word

我正在尝试制作一个Excel文档,可以粘贴单词列表进行搜索和替换。我想使用excel文档在单词文档中搜索和替换这些单词。

我在使代码起作用时遇到问题。

Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
i = Range("C2").Value  'pulls length of list from an excel function located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
    WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:\WordTest.docm"
WordDoc.Activate
With WordDoc.ActiveDocument
    For j = 1 To i
    With .Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWildcards = False
            .Wrap = wdFindContinue
            .Text = N(1, j)
            .Replacement.Text = N(2, j)
            .Execute
         End With
    End With
    Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub

我已根据建议更正了代码,并且不再遇到任何错误。但是,该代码似乎无法找到并替换指定的Word文档中的任何内容。我试图通过键入一个特定的单词“ text”和一个特定的单词“ replace”来查找和替换包含单词“ text”的单词文档中的代码,以简化查找和替换部分的代码。这仍然没有改变。我删除了关闭文档的行,以防万一问题是文档在更新后没有被保存,但这也没有成功,因此我将WordDoc.Quit重新添加到代码中。

Sub SearchReplace()

Dim WordDoc As Object, N As Variant, i As Integer, j As Integer

i = Range("C2").Value  'pulls length of list from an excel function 
located in cell C2
N = Range("B4:C" & CStr(i + 3)).Value
Set WordDoc = CreateObject(Class:="Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open Filename:="C:\WordTest.docm"
WordDoc.Documents("WordTest.docm").Activate
With WordDoc.ActiveDocument
For j = 1 To i
With .Range
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
        .Wrap = 1
        .Text = "text" 'N(j, 1)
        .Replacement.Text = "replace" 'N(j, 2)
        .Execute 2
     End With
End With
Next j
End With
WordDoc.Quit
Set WordDoc = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

“编译错误:未定义变量”问题是由于使用后期绑定和命名的Word常量引起的。要么改变

.Wrap = wdFindContinue

收件人:

.Wrap =  1

或插入:

Const wdFindContinue as Long = 1

位于代码的顶部。

无论如何,您现有的代码将不会替代任何内容。代替:

.Execute

您需要:

.Execute 2

或:

.Execute wdReplaceAll

(以替换所有实例)或:

.Execute 1

或:

.Execute wdReplaceOne

(仅替换第一个实例)

与wdFindContinue一样,您需要对wdReplaceAll或wdReplaceOne进行补充:

Const wdReplaceAll as Long = 2

或:

Const wdReplaceOne as Long = 1

视情况而定。

答案 1 :(得分:0)

我能够使代码起作用。该代码使用存储在代码数组中的excel工作表中的查找替换字符串列表(B4:B5004(查找),C4:C5005(替换))从Excel搜索指定的Microsoft Word文档。

要使代码正常运行,我必须包括Microsoft Word 16对象库。

从excel VBA编辑器窗口中。 然后,在“工具”>“引用”中确保选中了Microsoft Word 16.0对象库。

Option Explicit

Sub SearchReplace()

   Dim WordApp As Object, WordDoc As Object, N As Variant, i As Integer, j As Integer
   i = Range("C2").Value  'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
   N = Range("B4:C" & CStr(i + 3)).Value
   Set WordApp = CreateObject(Class:="Word.Application")
   Set WordDoc = WordApp.Documents.Open("C:\WordTest.docm")    
   WordDoc.Visible = True
   For j = 1 To i
        With WordApp
           With WordDoc.Content.Find
                .Text = N(j, 1)
                .Replacement.Text = N(j, 2)
                .Wrap = wdFindContinue
                .MatchWholeWord = True
                .Execute Replace:=wdReplaceAll
          End With
        End With
    Next j
    WordApp.ActiveDocument.Save
    WordApp.ActiveDocument.Close
    WordApp.Quit

    Set WordApp = Nothing
    Set WordDoc = Nothing
End Sub