Thank you for the reply!
I had to make one minor change - adding "vDirectory = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)" as the first line of the ErrHandler (see red text below). It now runs as intended.
I'm very new to programming and unfamiliar with functions, so I have one more question; I originally had RemoveHeadAndFoot as a Sub, not a function. I planned to call a series of Subs for various routines I needed to perform. I planned to comment them out if they were not needed (See blue text below).
Is there an easy way to incorporate them as subs to be run within the existing function or else define them as functions?
Code:Sub LoopDirectory() ' This code will allow the user to browse to a folder where MS Word documents reside. ' When executed to will loop through all documents ' Then run the referenced subs and save the modified files in a subdirectory called NewFiles. Application.FileDialog(msoFileDialogFolderPicker).Show vDirectory = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) If Len(Dir(vDirectory & "\" & "NewFiles", vbDirectory)) = 0 Then MkDir vDirectory & "\" & "NewFiles" MkDir vDirectory & "\" & "BadFiles" End If vfile = Dir(vDirectory & "\" & "*.*") Do While vfile <> "" Documents.Open FileName:=vDirectory & "\" & vfile ' 'Sub1 'Sub2 ' If the file isn't saved to the BadFiles Folder by RemoveHeadAndFoot ' then save it to the NewFiles Folder ' If RemoveHeadAndFoota = 0 Then ActiveDocument.SaveAs (vDirectory & "\" & "NewFiles" & "\" & ActiveDocument.Name) ActiveDocument.Close End If vfile = Dir Loop End Sub Function RemoveHeadAndFoot() ' This code will remove existing Headers and Footers in all documents in the folder. Dim oSec As Section Dim oHead As HeaderFooter Dim oFoot As HeaderFooter RemoveHeadAndFoota = 0 On Error GoTo ErrHandler For Each oSec In ActiveDocument.Sections For Each oHead In oSec.Headers If oHead.Exists Then oHead.Range.Delete Next oHead For Each oFoot In oSec.Footers If oFoot.Exists Then oFoot.Range.Delete Next oFoot Next oSec Exit Function ErrHandler: vDirectory = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) ActiveDocument.SaveAs (vDirectory & "\" & "BadFiles" & "\" & ActiveDocument.Name) ActiveDocument.Close ' ' Signal that the file has been saved ' RemoveHeadAndFoota = 1 End Function




Reply With Quote