Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 01-28-2020, 03:15 PM
Guessed's Avatar
Guessed Guessed is offline Static Folder Locations to Automate a Find and Replace using Word VBA Windows 10 Static Folder Locations to Automate a Find and Replace using Word VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Look carefully at this code and step through it line by line to work out what each line is doing. Your code sample shows a lot of useless activities going on. There are some key differences in this code when compared to yours and those differences can be important.
Code:
Sub bfMassReplaceLoopTest()
  Dim wDoc As Word.Document, i As Integer
  Dim fso As Object, mySource As Object, file As Object
  
  Application.ScreenUpdating = False
  On Error Resume Next
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set mySource = fso.GetFolder("C:\Users\rmoyar\Desktop\TestEnvironment\Macro Testing\Test 1")
  
  For Each file In mySource.Files       'loop through the directory
    If file.Name Like "*.doc*" And InStr(1, file.Name, "$") = 0 Then '$ is temp file mask
      i = i + 1
      Debug.Print i, file.Name
      Set wDoc = Documents.Open(FileName:=file, ConfirmConversions:=False, ReadOnly:=False, _
                AddToRecentFiles:=False, Visible:=True, Format:=wdOpenFormatAuto, XMLTransform:="")
      With wDoc.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = " 120 BF" 'Find What
        .Replacement.Text = " 125 BF" 'Replace With
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
      wDoc.Close SaveChanges:=True
    End If
  Next file
  Application.ScreenUpdating = True
  MsgBox "Task Complete, please check files", vbInformation
End Sub
Note that this code is only working on a single folder. You mentioned wanting this to work over ~100 folders but you need to make sure one folder is being done correctly first.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 

Tags
copy and paste, looping, word vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Folder Locations to Automate a Find and Replace using Word VBA Word find and replace ganesang Word 4 11-27-2018 12:04 AM
In Find and Replace, can Word stop after each Replace? wardw Word 1 06-08-2017 02:47 PM
Static Folder Locations to Automate a Find and Replace using Word VBA Find & replace footer text in a folder of Word 2010 documents kennethc Word 3 03-28-2015 02:49 AM
Static Folder Locations to Automate a Find and Replace using Word VBA Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Looking for a way to automate moving emails to folder middletree Outlook 1 09-28-2010 01:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:44 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft