Consulting

Results 1 to 3 of 3

Thread: Remove Header / Footer from files in folder

  1. #1

    Remove Header / Footer from files in folder

    Hi

    I am trying to loop through some pdf files in a folder to remove the incorrect header / footer information that was placed there. Ideally I would like whatever is there to be replaced by the correct information (but haven't worked out how to replace the info. yet)

    When I run the code below (thanks to Hans for the help), the pdf files can no longer be opened and I get the message "the file cannot be opened ... it may be corrupt" etc.

    Can anyone help?

    Sub RemoveHeaderFooter()
        Dim strFolder As String
        Dim strFile As String
        Dim wbk As Workbook
        Dim wsh As Worksheet
        ' Prompt for a folder
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strFolder = .SelectedItems(1)
                If Right(strFolder, 1) <> "\" Then
                    strFolder = strFolder & "\"
                End If
            Else
                MsgBox "You haven't specified a folder!", vbExclamation
                Exit Sub
            End If
        End With
        Application.ScreenUpdating = False
        ' Get the first filename
        strFile = Dir(strFolder & "*.pdf*")
        ' Loop
        Do While strFile <> ""
            ' Open workbook
            Set wbk = Workbooks.Open(strFolder & strFile)
            ' Clear headers and footers
            For Each wsh In wbk.Worksheets
                With wsh.PageSetup
                    .LeftHeader = ""
                    .CenterHeader = ""
                    .RightHeader = ""
                    .LeftFooter = ""
                    .CenterFooter = ""
                    .RightFooter = ""
                End With
            Next wsh
            ' Save and close workbook
            wbk.Close SaveChanges:=True
            ' Get next filename
            strFile = Dir
        Loop
        Application.ScreenUpdating = True
    End Sub

  2. #2
    If you add the line End to stop the process after the first file is opened, the problem becomes immediately obvious.
    Set wbk = Workbooks.Open(strFolder & strFile)
            End
    Excel is not compatible with PDF format. You cannot open a PDF file in Excel in order to edit it.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks gmayor. Back to the drawing board.

Posting Permissions

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