2016-11-20 134 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