View Single Post
 
Old 01-09-2016, 12:00 PM
jc491's Avatar
jc491 jc491 is offline Windows 10 Office 2016
VBA Novice
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default VBA Word - Search Within Files Containing A String - Copy Files to New Folder

Hello to all,

I am trying to work out how to Copy some word files.

I would like to search within files that contains the String or word [EDIT]
in a directory.

** The directory may have subfolders


If the file contains the string then - Copy the file to a new folder.


I found some code that I have been trying to adapt .

'http://www.vbaexpress.com/forum/archive/index.php/t-48591.html

Code:
Sub FindStringCopyFile()

' Find the  String [EDIT]  -  within Word file
' Copy File to new folder


Dim files
DocFile
Dim cDoc As Word.Document
nDoc As Word.Document
Dim cDocFN As String
SourceFolder As String
Dim DestinationPath As String
FileFolder As String

Dim nRng As Word.Range
cRng As Word.Range

Set nDoc = Documents.Add

SourceFolder = GetFolder & "\"
files = Dir(SourceFolder & "*.docx")

Do While files <> ""
Documents.Open SourceFolder & files
Set nRng = nDoc.Content

cDocFN = CStr(DocFile)
Documents.Open cDocFN

'        If Document contains String  [EDIT]

Set cRng = ActiveDocument.Content

If InStr(1, oPara.Range.Text, "[EDIT]") = 1 Then

    FileFolder.Copy DestinationPath
    
    End If

 Next FileFolder
    
  
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

files = Dir()
Loop

End Sub



Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.path
Set oFolder = Nothing
End Function
I would be really appreciative and grateful as always for the help and guidance. My code is messed now - it happens every time I add and remove new lines, variables - when looking for new ideas to improve the code.


To Summarize:

I am trying to achieve :

Search through documents in a directory that contain the string [EDIT] - within the document.

** Also The directory may have sub folders - to search through

COPY those files to a new folder.

- been stuck for 2 days now - not making any progress so any help welcome.

Thank you so much - for taking the time to help me again

J
Reply With Quote