【问题标题】:Find and Replace Multiple Text Strings on Multiple Text Files from a folder从文件夹中查找和替换多个文本文件中的多个文本字符串
【发布时间】:2018-03-31 03:52:23
【问题描述】:

我正在编写 vba 代码以完成以下任务

  1. Word 文档从文件夹中打开一个文本文件
  2. 根据 excel 表查找和替换文本(多个文本)(具有查找内容并替换为)
  3. 处理文件夹中的所有文本文件并保存。

我想为上述需求定制下面的代码,

我正在使用 Office 2016,我认为我必须将脚本中的 Application.FileSearch 替换为 ApplicationFileSearch 2003 和以前的 Office 版本。

我尝试使用宏记录器来完成,也使用了notepad++,我也用notepad++记录了它,它适用于一个文件,我想批量处理文件夹中的所有文件并在替换文本后保存.

由于要替换的行太多,要替换的行超过 30 行或更多,我希望代码能够从 excel 文件中查找文本,例如查找内容并替换为列。

 Sub FindReplaceAllDocsInFolder( )
 Dim i As Integer
 Dim doc As Document
 Dim rng As Range

 With Application.FileSearch
.NewSearch
.LookIn = "C:\My Documents"
.SearchSubFolders = False
.FileType = msoFileTypeWordDocuments
If Not .Execute( ) = 0 Then
    For i = 1 To .FoundFiles.Count
         Set doc = Documents.Open(.FoundFiles(i))
            For Each rng In doc.StoryRanges
             With rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Dewey & Cheatem"
                .Replacement.Text = "Dewey, Cheatem & Howe"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=wdReplaceAll
             End With
            Next rng
         doc.Save
         doc.Close

         Set rng = Nothing
         Set doc = Nothing
    Next i
Else
    MsgBox "No files matched " & .FileName
End If
 End With
 End Sub

谢谢 周杰伦

【问题讨论】:

    标签: vba excel ms-word


    【解决方案1】:

    借自https://social.msdn.microsoft.com/Forums/en-US/62fceda5-b21a-40b6-857c-ad28f12c1b23/use-excel-vba-to-open-a-text-file-and-search-it-for-a-specific-string?forum=isvvba

    Sub SearchTextFile()
        Const strFileName = "C:\MyFiles\TextFile.txt"
        Const strSearch = "Some Text"
        Dim strLine As String
        Dim f As Integer
        Dim lngLine As Long
        Dim blnFound As Boolean
        f = FreeFile
        Open strFileName For Input As #f
        Do While Not EOF(f)
            lngLine = lngLine + 1
            Line Input #f, strLine
            If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
                MsgBox "Search string found in line " & lngLine, vbInformation
                blnFound = True
                Exit Do
            End If
        Loop
        Close #f
        If Not blnFound Then
            MsgBox "Search string not found", vbInformation
        End If
    End Sub
    

    这只是找到匹配项。您可以使用内置功能“替换”来完成总修复。您还必须适应“循环文件”代码,这里是一个 sn-p。

    Dim StrFile As String
    StrFile = Dir(pathtofile & "\*" & ".txt")
    Do While Len(StrFile) > 0
        Debug.Print StrFile
        StrFile = Dir
    Loop
    

    我本来想把它写成评论的,但它的文字太多了。这并不意味着是一个完整的答案,只是给你你需要的部分,你需要自己把它们放在一起。

    【讨论】:

      【解决方案2】:

      感谢您的所有帮助。我找到了使用以下 EXE 的替代解决方案。 FART.exe(FART - 查找和替换文本)。我已经使用以下命令示例创建了一个批处理文件。

      https://emtunc.org/blog/03/2011/farting-the-easy-way-find-and-replace-text/

      http://fart-it.sourceforge.net/

      例子:

      放屁 "C:\APRIL2011\Scripts\someFile.txt" oldText newText

      这一行指示 FART 简单地将字符串 oldText 替换为 newText。

      fart -i -r "C:\APRIL2011\Scripts*.prm" March2011 APRIL2011

      此行将指示 FART 递归 (-r) 搜索 \Scripts 文件夹中扩展名为 .prm 的任何文件。 -i 标志告诉 FART 在查找字符串时忽略大小写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2013-11-28
        • 2013-01-29
        • 2015-05-14
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        相关资源
        最近更新 更多