【问题标题】:MS Word Find and Replace text within all docs in a folder using VBAMS Word 使用 VBA 在文件夹中的所有文档中查找和替换文本
【发布时间】:2013-10-11 08:30:20
【问题描述】:

我遇到了下面的代码,它搜索打开的 word 文档并在文档的所有区域 (StoryRanges) 内执行查找和替换。 它工作正常,但是我想问我如何修改此代码以查看所选文件夹中的所有文档并对该文件夹中的所有文档执行查找和替换?,而不仅仅是打开的活动文档?

我的计划是将宏分配给 Excel 中的一个按钮,以便用户可以单击该按钮,导航到该文件夹​​并立即对大量文档进行查找和替换。

我可以修改“IN ActiveDocument.StoryRanges”部分来查看文件夹吗?我不确定我可以修改它。顺便说一句...我是 vba 的新手,我正在努力研究和学习...我非常感谢您的时间、耐心和您在我试图找到自己的脚时可以提供的任何帮助 - 亚历克斯。

将 myStoryRange 调暗为范围

    For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.Find
        .Text = "Text to find to replace goes here"
        .Replacement.Text = "And the replacement text goes here"
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
    Do While Not (myStoryRange.NextStoryRange Is Nothing)
        Set myStoryRange = myStoryRange.NextStoryRange
        With myStoryRange.Find
            .Text = "Text to find to replace goes here"
            .Replacement.Text = "And the replacement text goes here"
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Loop
Next myStoryRange

【问题讨论】:

  • 通过使用 DIR 在 SO 或 Google 上进行搜索。你会发现很多例子。
  • 让我知道你是否仍然被卡住,我们会从那里拿走它:)
  • 感谢您的回复,我一直在研究DIR,也遇到了FileSystemObjects。我仍在思考并试图弄清楚我应该如何/&采用哪种方法。非常感谢

标签: vba excel ms-word


【解决方案1】:

我已经对代码进行了注释,因此您理解它应该没有任何问题。不过,如果你这样做,那么让我知道......

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
' This code uses Late Binding to connect to word and hence you '
' you don't need to add any references to it                   '
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

Option Explicit

'~~> Defining Word Constants
Const wdFindContinue As Long = 1
Const wdReplaceAll As Long = 2

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object, rngStory as Object
    Dim sFolder As String, strFilePattern As String
    Dim strFileName As String, sFileName As String

    '~~> Change this to the folder which has the files
    sFolder = "C:\Temp\"
    '~~> This is the extention you want to go in for
    strFilePattern = "*.docx"

    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oWordApp.Visible = True

    '~~> Loop through the folder to get the word files
    strFileName = Dir$(sFolder & strFilePattern)
    Do Until strFileName = ""
        sFileName = sFolder & strFileName

        '~~> Open the word doc
        Set oWordDoc = oWordApp.Documents.Open(sFileName)

        '~~> Do Find and Replace
        For Each rngStory In oWordDoc.StoryRanges
            With rngStory.Find
                .Text = "Text to find to replace goes here"
                .Replacement.Text = "And the replacement text goes here"
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next

        '~~> Close the file after saving
        oWordDoc.Close SaveChanges:=True

        '~~> Find next file
        strFileName = Dir$()
    Loop

    '~~> Quit and clean up
    oWordApp.Quit

    Set oWordDoc = Nothing
    Set oWordApp = Nothing
End Sub

【讨论】:

  • 非常感谢! , 非常有帮助。我刚刚尝试过,它确实会遍历文件并进行替换,但是它不会像我发布的初始编码那样替换文档的所有部分。我需要查找和替换页眉和页脚以及文档主体中的文本。 'MyStoryRange' 可以做到,但我只是看不到如何让它查看整个文档文件夹,而不仅仅是活动文档。
  • 非常感谢悉达多!!
  • 遗憾的是它不会在表格和文档的其他部分中找到单词。
猜你喜欢
  • 2017-08-06
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2015-01-31
  • 2021-04-27
  • 1970-01-01
  • 2021-03-13
相关资源
最近更新 更多