在我们的工作中,处理excel时难免遇到打印问题。顺着打印点击即可,但有时候会遇到需要隔页打印的情况,下面分享几个隔页打印的代码,带注释。
' StartPage 要打印的起始页
' TotalPages 要打印的总页数
' SkipPages 每次打印跳过的页数
' Invert 是否逆序
Sub PrintSheet(ByVal StartPage As Integer, _
ByVal TotalPages As Integer, _
ByVal SkipPages As Integer, _
ByVal Invert As Boolean)
Dim s As Integer
s = IIf(Invert, -(SkipPages + 1), SkipPages + 1)
For i = StartPage To TotalPages Step s
ActiveWindow.SelectedSheets.PrintOut From:=i, To:=i
Next i
End Sub
Sub PrintPosAndNegPages()
TotalPages = ExecuteExcel4Macro("Get.Document(50)")
' 打印正面
PrintSheet 1, TotalPages, 1, False
' 暂停打印
MsgBox "请将打印出的纸张反向装入纸槽中", vbOKOnly, "打印另一面"
' 打印反面,从最后的TotalPages打印到第二页
' 判断总页数是否为奇数
Dim IsOdd As Boolean
IsOdd = CBool((TotalPages Mod 2) = 1)
If IsOdd Then
' 奇数页,最后一页空白,需要手动取出
MsgBox "请将打印出的纸张最后一页取出,然后确定并继续", vbOKOnly, "打印另一面"
End If
' 向下取偶
FromPages = TotalPages - (TotalPages Mod 2)
PrintSheet FromPages, 2, 1, True
End Sub
______________________________________________________________________________________
代码2:
Sub 隔页打印()
Dim i As Byte
Dim j as Byte
j=ActiveSheet.HPageBreaks.Count+1
For i = 2 To j Step 2 ' 偶数页
ActiveSheet.PrintOut from:=i, To:=i
Next i
End Sub
Sub 隔页打印()
Dim i As Byte
For i = 1 To ActiveSheet.HPageBreaks.Count Step 2 '奇数页
ActiveSheet.PrintOut from:=i, To:=i
Next i
End Sub