I have a number of documents in a folder. They are in a variety of formats. I have written a macro to loop through them one at a time, modify the format and save the file to a "NewFiles" folder. Occasionally a file has a problem and halts the code due to an error. I want to place them in a folder called "BadFiles", but I cant seem to get it to work. Any help is appreciated!
Here is the code thus far:
- 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)
- On Error GoTo Err:
- 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
- RemoveHeadAndFoot 'Call sub from below
- ActiveDocument.SaveAs (vDirectory & "\" & "NewFiles" & "\" & ActiveDocument.Name)
- ActiveDocument.Close
- vFile = Dir
- Loop
- End Sub
- Err:
- If Err.Number <> 0 Then
- msg "Error: " & Err.Number
- Err.Clear
- ActiveDocument.SaveAs (vDirectory & "\" & "BadFiles" & "\" & ActiveDocument.Name)
- ActiveDocument.Close
- End If
- End sub
- Sub 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
- 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
- End Sub

