+ Reply to Thread
Results 1 to 2 of 2

Find and replace to loop through subfolders too

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-09-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    102

    Find and replace to loop through subfolders too

    Hi,

    I found this code online and I would like to be able to perform the find and replace on subfolders too. Is it possible?

    Sub FindAndReplaceInFolder()
      Dim objDoc As Document
      Dim strFile As String
      Dim strFolder As String
      Dim strFindText As String
      Dim strReplaceText As String
     
      '  Pop up input boxes for user to enter folder path, the finding and replacing texts.
      strFolder = InputBox("Enter folder path here:")
      strFile = Dir(strFolder & "\" & "*.docx", vbNormal)
      strFindText = InputBox("Enter finding text here:")
      strReplaceText = InputBox("Enter replacing text here:")
     
      '  Open each file in the folder to search and replace texts. Save and close the file after the action.
      While strFile <> ""
        Set objDoc = Documents.Open(FileName:=strFolder & "\" & strFile)
        With objDoc
          With Selection
            .HomeKey Unit:=wdStory
            With Selection.Find
              .text = strFindText
              .Replacement.text = strReplaceText
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
          End With
          objDoc.Save
          objDoc.Close
          strFile = Dir()
        End With
      Wend
    End Sub
    Thanks

    Z

  2. #2
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: Find and replace to loop through subfolders too

    Is it possible?
    Yes.
    For my opinion the code must be decomposed to several. 1. main code , 2 Subfolders enumeratre that must be recursive and 3 file processing
    For subfolders you can use
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Program Files")
    For Each SubFolder In Folder.SubFolders
        debug.print SubFolder.Name
    Next

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find and Replace Loop + a Loop to remove Duplicate values
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2018, 07:43 AM
  2. Find&Replace through folders and subfolders macro looping more than necessary
    By NickZul in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2018, 01:21 AM
  3. Find and Replace Loop does not find all and loop properly
    By mattyfaz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2015, 02:15 AM
  4. [SOLVED] Loop - Find and Replace
    By ELeGault in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-10-2015, 06:45 PM
  5. Excel 2007 : Find and replace loop
    By chrisrabkin in forum Excel General
    Replies: 1
    Last Post: 10-18-2011, 05:58 PM
  6. [SOLVED] copy subfolders, replace text in files and save files in copied subfolders
    By pieros in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2005, 09:05 AM
  7. Find & Replace / Loop & Vlookup
    By thom hoyle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-24-2005, 08:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1