Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2020, 12:41 PM
rmoyar rmoyar 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 2019
Novice
Static Folder Locations to Automate a Find and Replace using Word VBA
 
Join Date: Jan 2020
Posts: 4
rmoyar is on a distinguished road
Talking Static Folder Locations to Automate a Find and Replace using Word VBA

First Time poster here, thank you all for the help in advance. I am very stuck on this situation. I have modified (started with Do While - lack of experience = dead end) several times.

This is the closest I think I have been, but basically I have truncated the code a bit... There are 100 different " xx BF" replacements that will need to happen on ~ 100 different Folder locations (want to hit the button and retire for the night).

immediate window indicates

1
2
1
2
2
2

So it hits the first Debug.Print = 1, then doesn't print the file.name but still proceeds and prints the second debug.print i = 2 (after the ++)

But then goes into the next for loop? There are 10 .doc files in the listed file path for testing

[Sub bfMassReplaceLoopTest()

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim mySource As Object
Dim myVar(0 To 2000) As String

Application.ScreenUpdating = False
On Error Resume Next
Set mySource = obj.GetFolder.Items.Item.path("C:\Users\rmoyar\Des ktop\TestEnvironment\Macro Testing\Test 1")

i = 1

For Each file In mySource.Files 'loop through the directory
If Len(file.Name) > 0 And InStr(1, file.Name, "$") = 0 Then '$ is temp file mask
Debug.Print i

myVar(i) = file.Name
i = i + 1

Debug.Print file.Name
Debug.Print i
End If
Next file

j = 1

For j = 1 To i Step 1
Debug.Print j
Debug.Print i


Set Doc = Documents.Open(FileName:=myVar(j), ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, Visible:=True, Format:=wdOpenFormatAuto, XMLTransform:="")
Windows(myVar(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting


With Selection.Find
.Text = " 120 BF" 'Find What
.Replacement.Text = " 125 BF" 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.Close SaveChanges:=True
Next
Application.ScreenUpdating = True
MsgBox "Task Complete, please check files", vbInformation
Debug.Print " "
Debug.Print " "
Debug.Print " "
Debug.Print " "
End Sub
][/CODE]
Reply With Quote
  #2  
Old 01-28-2020, 02:59 PM
gmaxey gmaxey 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: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Why not see if this works:
VBA Find & Replace
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #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: 3,969
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
  #4  
Old 01-28-2020, 03:18 PM
gmaxey gmaxey 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: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If the other 99 folders are sub-folders of the first then the add-in posted earlier may still work.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 01-28-2020, 05:03 PM
macropod's Avatar
macropod macropod is offline Static Folder Locations to Automate a Find and Replace using Word VBA Windows 7 64bit Static Folder Locations to Automate a Find and Replace using Word VBA Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See also: https://www.msofficeforums.com/word-...ocx-files.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 01-29-2020, 05:57 AM
rmoyar rmoyar 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 2019
Novice
Static Folder Locations to Automate a Find and Replace using Word VBA
 
Join Date: Jan 2020
Posts: 4
rmoyar is on a distinguished road
Default

First off, thank you all for your assistance.

@guessed...

I had a question about the fso.GetFolder. If I were to add a block of code after the For loop, and changed the mySource to

Set mySource = fso.GetFolder("C:\Users\rmoyar\Desktop\TestEnviron ment\Macro Testing\Test 2")

And then reran the for loop, would that produce the same functionality after completing folder 'Test 1' for 'Test 2'?

I think you and I were having the same ideas. I was planning on making it work for one, and then calling separate subs for each task. Was planning on making an array that would house the paths of each folder location, and step through that (let's say in a k variable incremental fashion), and pass that back as the variable mySource in the future development, and then in the main sub would call another sub, call it the 'loopSub' that would do the For each file in mySource.Files.

this is certainly exciting for me, as it is well beyond capabilities I have done before, and I am more in favor of learning how it works so that I may apply these skills to other things more readily. I saw the plug-in that @gmaxey mentioned, and again, I think I would prefer to learn myself (may in the future recommend this to others, but for now just wanted to take this as an educational opportunity).

Again, thank you all!

TL;DR version - if I have another line that is mySource = fso.GetFolder(<Test 2\>) will that then process Test 2 files after Test 1 is completed?
Reply With Quote
  #7  
Old 01-29-2020, 06:10 AM
rmoyar rmoyar 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 2019
Novice
Static Folder Locations to Automate a Find and Replace using Word VBA
 
Join Date: Jan 2020
Posts: 4
rmoyar is on a distinguished road
Default

I opened a document in the folder after getting the MsgBox, and it has not been updated... still not sure why, but getting the same error.

Immediate Window indicates all files, and correct file.Name (sans extension).

I am under the impression that the .docx shouldn't matter because the wildcard used.
Reply With Quote
  #8  
Old 01-29-2020, 06:43 AM
rmoyar rmoyar 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 2019
Novice
Static Folder Locations to Automate a Find and Replace using Word VBA
 
Join Date: Jan 2020
Posts: 4
rmoyar is on a distinguished road
Default

Further Update, when downloading and attempting to run the VBA Find and Replace tool linked, I run Windows 10, Word 2019. It prompts me with an error that the macro can't be found or has been disabled due to security settings (googled and tried all suggestions with renaming/etc.)
Reply With Quote
  #9  
Old 01-29-2020, 08:12 AM
gmaxey gmaxey 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: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Hmm, that could be a Office 64 issue and I don't have it. I've looked at the declarations and I think have them updated for Office 64 bit application. Please download the package again and let me know. The date of the file should be 1/29/2020.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 01-29-2020, 02:35 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: 3,969
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

The find and replace tool that Greg has pointed you at is by far the easiest way to do this. Hopefully you can get that working.

As a learning exercise, read carefully the thread that Macropod pointed you at, paying careful attention to the evolution of the requirements and how that changed the code. Copy the separate code blocks and step through the code each time to learn how it works and what each loop is progressing through.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

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 11:14 PM.


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