excel to pdf ignores print area and calls printer

后端 未结 1 858
逝去的感伤
逝去的感伤 2020-11-29 14:21

This loops through a list of students, but fails on the print area, which is set and coded in the export line - it prints 130 pages for each student when it should only be o

相关标签:
1条回答
  • 2020-11-29 14:56

    so I changed track - excel vba does not seem to be happy producing pdf files with the printer set as it is... So, i changed to export an excel file per student using copy & paste special values and formats. Here is the code i did (lots stolen from other answers on here! thanks...) Any comments about improving the code are welcome - I think there is a lot of scope for that!!

        Option Explicit
    
    Sub Exportmacro()
        Dim rCell As Range, rRng As Range 'define loop names
        Dim NewCaseFile As Workbook 'give a name to new work book for duplicate sheet
        Dim wks As Worksheet 'name of the copy of feedback
        Dim sPath As String
        sPath = MacScript("(path to desktop folder as string)")
    'turn off screen
    With Application
    '        .ScreenUpdating = False  ‘only removed while testing
    '        .EnableEvents = False
    '        .Calculation = xlCalculationManual  ‘disabled for the moment
    End With
    
        'Student numbers in cells A7:A160 WARNING SET TO 3 STUDENTS ONLY FOR TEST
        Set rRng = Worksheets("studentlist").Range("A7:A9")
    
        With Worksheets("Feedback") '<--| reference "Feedback" worksheet
    
            For Each rCell In rRng '<--| loop through "students" range
                .Range("A1").Value = rCell.Value '<--| write current student number to cell A1 on Feedback sheet
    
               'do copy ready for paste spec vals to destroy links & calculations
                   ActiveSheet.Range("A2:W77").Copy
    
                'now open new workbook then pastespecial values and formats
                 Set NewCaseFile = Workbooks.Add
                 NewCaseFile.Sheets(1).Range("A1").PasteSpecial xlPasteValues
                 NewCaseFile.Sheets(1).Range("A1").PasteSpecial xlPasteFormats
    
                'now save as xls with student number as filename Filename:=sPath & rCell.Value & ".xlsx"
                 ActiveWorkbook.SaveAs Filename:=rCell.Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    
                'now close duplicate file
                 ActiveWorkbook.Close False
    
            Next rCell   '<-- next student number
        End With         '<-- once all done
    'turn screen back on
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    
    0 讨论(0)
提交回复
热议问题