|
本帖最后由 cbtaja 于 2017-1-12 19:25 编辑
再稍修改,添加对预定文件夹是否存在的判断、处理代码:If Dir([o2]) = "" Then MkDir [o2]
- Sub 每一页各自打印成不同的PDF文件()
- Dim fw$, fs&, endrow&, r&, phzx$, PrinterApplied As Boolean
- If MsgBox("是否确定打印?", vbOKCancel) <> vbOK Then Exit Sub
- If Dir([o2]) = "" Then MkDir [o2]
- fs = Sheet1.[m2]
- If fs < 1 Then Exit Sub
- endrow = Sheet2.[b65536].End(3).Row
- fw = Replace("q" & Sheet1.[m1], "-", ":q")
- For Each cel In Sheet2.Range(fw)
- r = cel.Row
- If r >= 5 And r <= endrow Then
- phzx = cel.Value
- Sheet1.[j1] = phzx
- DoEvents
- fileFullName = [o2] & IIf(Right([o2], 1) = "", "", "") & [q1] & ".pdf"
- fileFullName = Replace(fileFullName, "#", "-")
-
- If Application.Version >= "12" Then '适用于把多次打印内容打印到一个PDF文档内,条件是安装有EXCEL为2007或更高的版本。
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
- fileFullName, Quality:=xlQualityStandard, _
- IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
- False
- Else
- MsgBox "必须安装EXCEL2007或更高版本!": Exit Sub
- End If
- End If
- Next
- MsgBox "完成!"
- End Sub
- Sub 所有页面合并打印一个PDF文档中()
- Dim fw$, fs&, endrow&, r&, phzx$, PrinterApplied As Boolean
- If MsgBox("是否确定打印?", vbOKCancel) <> vbOK Then Exit Sub
- Application.Dialogs(xlDialogPrinterSetup).Show
- PrinterApplied = InStr(UCase(ActivePrinter), "PDF")
- If Dir([o2]) = "" Then MkDir [o2]
- fs = Sheet1.[m2]
- If fs < 1 Then Exit Sub
- endrow = Sheet2.[b65536].End(3).Row
- fw = Replace("q" & Sheet1.[m1], "-", ":q")
- For Each cel In Sheet2.Range(fw)
- r = cel.Row
- If r >= 5 And r <= endrow Then
- phzx = cel.Value
- Sheet1.[j1] = phzx
- DoEvents
-
- fileFullName = [o2] & IIf(Right([o2], 1) = "", "", "") & [q1]
- fileFullName = Replace(fileFullName, "#", "_")
-
- If PrinterApplied Then _
- Sheet1.PrintOut copies:=fs _
- Else _
- MsgBox "当前所选的打印机不是PDF虚拟打印机!": Exit Sub '适用于把多次打印内容打印到一个PDF文档内,必须安装有虚拟打印机。
- End If
- Next
- MsgBox "完成!"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|