Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2016, 12:00 PM
jc491's Avatar
jc491 jc491 is offline VBA Word - Search Within Files Containing A String - Copy Files to New Folder Windows 10 VBA Word - Search Within Files Containing A String - Copy Files to New Folder Office 2016
VBA Novice
VBA Word - Search Within Files Containing A String - Copy Files to New Folder
 
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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Word - Search Within Files Containing A String - Copy Files to New Folder Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
VBA Word - Search Within Files Containing A String - Copy Files to New Folder Movable Word template with linked files and hidden folder Elmobram22 Word 6 11-15-2013 02:11 PM
How to copy linked Excel and Word files and retain links ashleynpeters1 Word 1 05-30-2013 02:25 PM
VBA Word - Search Within Files Containing A String - Copy Files to New Folder Opening Word /Excel reinstalls files, loses recent files adj1 Office 3 05-10-2013 12:27 AM
VBA Word - Search Within Files Containing A String - Copy Files to New Folder Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:10 AM.


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