Results 1 to 10 of 10

Thread: Error Trap Help

Threaded View

  1. #9

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    13

    Re: Error Trap Help

    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
    Last edited by Hack; May 5th, 2011 at 10:51 AM. Reason: Added Code Tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width