PDA

View Full Version : Create code to search through folders and execute mass find & replace



rybozome
04-17-2013, 02:59 PM
First post, so I apologize if I miss any vbaexpress etiquette. I am currently in the process of updating a large amount of MS Word documents that reference other MS Word documents in them (because we changed the name of a bunch of the documents).

I have written a code that I would like to search a certain folder directory and go through and open each document to execute the find & replace, but it does not seem to be working. As soon as it hits my "Do While", it just skips the whole loop entirely and goes to End Sub. What am I doing wrong? Thanks in advance. Here is what I currently have:

Sub MassReplace()

Dim strPath As String
Dim strFile As String
Dim doc As Document

Application.ScreenUpdating = False

strPath = InputBox("Insert filepath you would like to search")
strFile = Dir(strPath + "*.docx")

Do While strFile <> ""
Documents.Open (strPath & strFile)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = "docname"
.MatchCase = True
.Replacement.Text = "newdocname"
End With
Selection.Find.Execute Replace:=wdReplaceAll
doc.Close True

Loop
Application.ScreenUpdating = True

End Sub

fumei
04-17-2013, 05:20 PM
Bottom line? strPath is not a qualified path. Please note that as you are entering it as text, strPath MUST MUST MUST end with a path Separator (\). If it is not, then it is not fully qualified.

rybozome
04-17-2013, 07:03 PM
So do you mean that when whoever puts the file path in the Input Box to define strPath it must end with a (\)?

C:\Documents and Settings\Owner\My Documents\

as opposed to

C:\Documents and Settings\Owner\My Documents

??

fumei
04-17-2013, 08:01 PM
Yes, exactly. Otherwise you get :


C:\Documents and Settings\Owner\My Documents*.docx

as the DIR action search (i.e. a file named My Documents*.docx, in the Owner folder), NOT

C:\Documents and Settings\Owner\My Documents\*.docx
(all docx files in the My Documents folder).

fumei
04-17-2013, 08:14 PM
You may want to try something like:
Function PickFolder(strStartDir) As String
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.Application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.Items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function


Sub MassReplace()

Dim strPath As String
Dim strFile As String
Dim doc As Document

Application.ScreenUpdating = False

strPath = PickFolder("c:\")
strFile = Dir(strPath + "*.docx")

Do While strFile <> ""
Documents.Open (strPath & strFile)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = "docname"
.MatchCase = True
.Replacement.Text = "newdocname"
End With
Selection.Find.Execute Replace:=wdReplaceAll
doc.Close True
Loop
Application.ScreenUpdating = True
End SubSome important notes.

1. You can put any folder in as the starting folder, it is does not have to be C:\

2. However, as it stands, IN the dialog you can not switch drives - say C: to X: You can of course use a different drive as the starting point, say

strPath = PickFolder("X:\Yadda\Whatever")

3. If you put "" as the starting folder:

strPath = PickFolder("")

You CAN select a drive, and of course the folders under it.

gmaxey
04-17-2013, 09:24 PM
Alot of the work is done for you here: http://gregmaxey.mvps.org/word_tip_pages/vba_find_and_replace.html

rybozome
04-17-2013, 10:35 PM
Great, I will give all these suggestions a shot tomorrow morning and let you guys know the outcome. Thanks!