look in several folders for a single document with a specific name

R

ryguy7272

I have used the macro below with great success! I am curious to know how to
modify the code to get the macro to look in several folders for a single
document. For instance, I may have Folder#1, Folder#2, Folder#3, Folder#4,
etc., and maybe as many as 50 different folders within one folder. How can I
get the macro to search through each of these folders and check for a certain
document, with a name such as “Coverage Terms� If a document with a certain
name is found, I’d like to copy/paste, append, whatever, to my main document
(that I run the macro from). I guess I need a line or two of code right
after the line:
..LookIn = "F:\Microsoft Word"



Sub Append()
Dim i As Long
Application.ScreenUpdating = False
Documents.Add
With Application.FileSearch
'Search in foldername
..LookIn = "F:\Microsoft Word"
..SearchSubFolders = False
..FileName = "*.doc"
..Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "~") = 0 Then
Selection.InsertFile FileName:=(.FoundFiles(i)), _
ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdPageBreak
End If
Next i
End With
End Sub
 
R

ryguy7272

Got it with 2 extremely minor changes:
..SearchSubFolders = True
..FileName = "Updates*.doc"


Sub Append()
Dim i As Long
Application.ScreenUpdating = False
Documents.Add
With Application.FileSearch
..LookIn = "F:\Microsoft Word"
..SearchSubFolders = True
..FileName = "Updates*.doc"
..Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "~") = 0 Then
Selection.InsertFile FileName:=(.FoundFiles(i)), _
ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdPageBreak
End If
Next i
End With
End Sub


I'm more used to VBA in Excel; VBA in word is still quite intimidating for me.
Hope this (simple) solution helps others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top