2016-11-20 235 views
0

这会遍历学生列表,但在输出行中设置并编码的打印区域中失败 - 它为每个学生打印130页,当它只应该是一个。所有的打印机被调用,打开一个对话框(登录6密码),并停止宏 - 打印机是网络上的工作打印机,并不总是可用的。 有没有办法停止打印机被叫? 并控制页面到打印区域?excel to pdf忽略打印区域并呼叫打印机

Option Explicit 

Sub PdfExportMacro() 
Dim rCell As Range, rRng As Range 

'Student numbers in cells A7:A160 
Set rRng = Worksheets("studentlist").Range("A7:A160") '<--| set your "students" range 

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 

     ' Export & save file as pdf using SNum as filename: 
     .ExportAsFixedFormat Type:=xlTypePDF, fileName:= _ 
     "Macintosh HD:Users:Michael:Desktop:" & rCell.Value, Quality:= _ 
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _ 
     OpenAfterPublish:=False 
    Next rCell 
End With 

End Sub 
+0

您在那里的代码,它将始终将反馈复制到反馈表单元格A1中,然后将其导出为新文件。这意味着您将拥有130个PDF文件。这是你想要的吗?你还使用什么打印代码? – Niclas

+0

嗨Niclas,谢谢你看这个。复制到单元格A1的值是学生列表表单中的学生编号 - 该循环工作正常,发生的情况是所创建的pdf - 反馈表中应该只有1页(定义了打印区域),而是全部130页在床单上的数据(这是巨大的......)。如果我理解正确,唯一的打印代码是导出格式语句。 –

+0

不知道我明白。目前的代码将创建130个PDF文件,而不是130个页面。它会将学生编号复制到反馈表中的A1,然后将其保存为“student number.pdf”。所以这是错误的根据你想达到什么?什么是打印区域? – Niclas

回答

0

,所以我改变轨道 - Excel VBA中似乎并不乐意生产与打印机设置的PDF文件,因为它是... 所以,我改变了使用复制粘贴&特别出口每名学生一个excel文件值和格式。这里是我所做的代码(很多从这里的其他答案被盗!谢谢...)任何关于改进代码的意见都是值得欢迎的 - 我认为这有很大的范围!

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