If Err.Number = 1004 Then
MsgBox "打印时发生错误,请检查打印机设置", 0 + 48 '提示用户发生打印错误
Exit Sub
End If
Next i
End Sub
Sub OddPagePrint(control As IRibbonControl) '打印奇数页
On Error Resume Next
Dim TotalPageNums, i As Integer
TotalPageNums = ExecuteExcel4Macro("Get.Document(50)")
If (TotalPageNums = 0) Then '如果为零,说明没有可打印内容,退出程序
MsgBox "Microsoft Excel 未发现任何可以打印的内容", 0 + 48
Exit Sub
End If
For i = 1 To Int(TotalPageNums / 2) + 1
ActiveWindow.SelectedSheets.PrintOut From:=2 * i - 1, To:=2 * i - 1
If Err.Number = 1004 Then
MsgBox "打印时发生错误,请检查打印机设置", 0 + 48 '提示用户发生打印错误
Exit Sub
End If
Next i
End Sub
Sub EvenPagePrint(control As IRibbonControl) '打印偶数页
On Error Resume Next
Dim TotalPageNums As Integer, i As Integer
TotalPageNums = ExecuteExcel4Macro("Get.Document(50)")
If TotalPageNums = 0 Then '如果为零,说明没有可打印内容,退出程序
MsgBox "Microsoft Excel 未发现任何可以打印的内容", 0 + 48
Exit Sub
End If
If TotalPageNums = 1 Then
MsgBox ("只有第一页!!!")
End
End If
For i = 1 To Int(TotalPageNums / 2) + 1
ActiveWindow.SelectedSheets.PrintOut From:=2 * i, To:=2 * i
If Err.Number = 1004 Then
MsgBox "打印时发生错误,请检查打印机设置", 0 + 48 '提示用户发生打印错误
Exit Sub
End If
Next i
End Sub
Sub CurrentPagePrint(control As IRibbonControl)
On Error Resume Next
Dim m As Integer, n As Integer
Dim vPageBreaksCount As Integer, hPageBreaksCount As Integer
Dim NumPage As Integer, TotalPages As Integer
TotalPages = ExecuteExcel4Macro("Get.Document(50)") '总的打印页数
If TotalPages = 0 Then
MsgBox "Microsoft Excel 未发现任何可以打印的内容", 0 + 48
Exit Sub
End If
hPageBreaksCount = ActiveSheet.HPageBreaks.Count '返回水平分页符个数
vPageBreaksCount = ActiveSheet.VPageBreaks.Count '返回垂直分页符个数
For n = 1 To hPageBreaksCount 'n返回当前水平分页符的数量
If ActiveSheet.HPageBreaks(n).Location.Row > ActiveCell.Row Then Exit For
Next
For m = 1 To vPageBreaksCount 'm返回当前垂直分页符的数量
If ActiveSheet.VPageBreaks(m).Location.Column > ActiveCell.Column Then Exit For
Next
If ActiveSheet.PageSetup.Order = xlOverThenDown Then '按页面设置中的打印顺序确定当前单元格所处的页数
NumPage = (n - 1) * (vPageBreaksCount + 1) + m '先行后列
Else
NumPage = (m - 1) * (hPageBreaksCount + 1) + n '先列后行
End If
'判断选定的单元格是否在打印区域中
If ActiveCell.Value = "" Then '如果选定的单元格为空
ActiveCell.Value = 1 '给活动单元格赋予临时数值
If ExecuteExcel4Macro("Get.Document(50)") > TotalPages Or NumPage > TotalPages Then '如果总的打印页数增多或单元格所处页数大于打印页数
ActiveCell.Value = ""
MsgBox "选定的单元格不在打印范围以内!"
Exit Sub
Else
ActiveCell.Value = ""
ActiveSheet.PrintOut From:=NumPage, To:=NumPage, Copies:=1
If Err.Number = 1004 Then
MsgBox "打印时发生错误,请检查打印机设置", 0 + 48 '提示用户发生打印错误
Exit Sub
End If
Exit Sub
End If
Else
ActiveSheet.PrintOut From:=NumPage, To:=NumPage, Copies:=1
If Err.Number = 1004 Then
MsgBox "打印时发生错误,请检查打印机设置", 0 + 48 '提示用户发生打印错误
Exit Sub
End If
End If
End Sub
然后单击VBA编辑器的“保存”按钮保存工程。
四、添加到Excel加载项中
如果不是经常进行双面打印,可以在每次打印前打开“Excel2007双面打印工具.xlam”文件,然后进行双面打印即可。
如果需要经常进行双面打印,可以让Excel每次运行时,都自动加载“Excel2007双面打印工具.xlam”文件,使自定义的选项卡总是出现在Excel 2007的功能区中。这必需把上述文件添加到Excel加载项中。步骤如下:
1.单击“Office 按钮”,再单击“Excel 选项”。
2.在“Excel选项”对话框中单击“加载项”。
3.直接单击“转到”,弹出“加载项”窗口。
4.单击“浏览”,查找并选择“Excel2007双面打印工具.xlam”文件,单击“确定”。
|