Vba 打印到PDF不起作用
假设我的代码检查单元格j2中的电子邮件地址,如果找到,将该特定选项卡转换为pdf,并将其保存在用户选择的文件路径中。它在我制作宏的原始工作簿上运行良好。当我复制代码并尝试运行它时,它会将不同的表单打印到pdf中,而这些表单在j2中甚至没有任何具有错误选项卡名称的表单。在打印pdf行上运行代码时,我不断收到运行时错误5无效过程调用或参数Vba 打印到PDF不起作用,vba,excel,Vba,Excel,假设我的代码检查单元格j2中的电子邮件地址,如果找到,将该特定选项卡转换为pdf,并将其保存在用户选择的文件路径中。它在我制作宏的原始工作簿上运行良好。当我复制代码并尝试运行它时,它会将不同的表单打印到pdf中,而这些表单在j2中甚至没有任何具有错误选项卡名称的表单。在打印pdf行上运行代码时,我不断收到运行时错误5无效过程调用或参数 Sub SaveSheetsAsPDF() Dim DestFolder As String Dim PDFFile As String Dim wb As W
Sub SaveSheetsAsPDF()
Dim DestFolder As String
Dim PDFFile As String
Dim wb As Worksheet
Dim AlwaysOverwritePDF As Boolean
'Speed up macro
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Prompt for file destination
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = True Then
DestFolder = .SelectedItems(1)
Else
MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
Exit Sub
End If
End With
'Create new PDF file name including path and file extension
For Each wb In ThisWorkbook.Worksheets
'Test j2 for a mail address
If wb.Range("J2").Value Like "?*@?*.?*" Then
PDFFile = DestFolder & Application.PathSeparator & wb.Name & "-" & Format(Date, "mmyy") & ".pdf"
'If the PDF already exists
If Len(Dir(PDFFile)) > 0 Then
If AlwaysOverwritePDF = False Then
OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
'If you want to overwrite the file then delete the current one
If OverwritePDF = vbYes Then
Kill PDFFile
Else
MsgBox "OK then, if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
Else
On Error Resume Next
Kill PDFFile
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If
End If
'Prints PDF
wb.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next wb
MsgBox "All Files Have Been Converted!"
ResetSettings:
'Resets optimization settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub-SaveSheetsAsPDF()
将文件夹设置为字符串
将PDFFile设置为字符串
将wb设置为工作表
Dim AlwaysOverwritePDF作为布尔值
“加速宏”
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
'提示输入文件目标
使用Application.FileDialog(msoFileDialogFolderPicker)
如果.Show=True,则
DestFolder=.SelectedItems(1)
其他的
MsgBox“您必须指定一个文件夹以将PDF保存到其中。”&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“必须指定目标文件夹”
出口接头
如果结束
以
'创建新的PDF文件名,包括路径和文件扩展名
用于此工作簿中的每个wb。工作表
'测试邮件地址的j2
如果wb.范围(“J2”).值像“*@?*.?*”那么
PDFFile=DestFolder&Application.PathSeparator&wb.Name&“-”&Format(日期,“mmyy”)和“.pdf”
'如果PDF已经存在
如果Len(Dir(PDFFile))>0,则
如果AlwaysOverwritePDF=False,则
OverwritePDF=MsgBox(PDFFile&“已存在”。&vbCrLf&vbCrLf&“是否要覆盖它?”,vbYesNo+vbQuestion,“文件存在”)
出错时继续下一步
'如果要覆盖该文件,请删除当前文件
如果覆盖EPDF=vbYes,则
杀死Pdfile
其他的
MsgBox“好的,如果你不覆盖现有的PDF,我就不能继续。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“退出宏”
出口接头
如果结束
其他的
出错时继续下一步
杀死Pdfile
如果结束
如果错误号为0,则
MsgBox“无法删除现有文件。请确保该文件未打开或未受写保护。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“无法删除文件”
出口接头
如果结束
如果结束
如果结束
'打印PDF
wb.ExportAsFixedFormat类型:=xlTypePDF,文件名:=Pdfile,质量:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas:=False,OpenAfterPublish:=False
下一个wb
MsgBox“所有文件都已转换!”
重置设置:
'重置优化设置
Application.EnableEvents=True
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
端接头
编辑:也不是工作簿上的所有工作表都需要转换。因此,只有需要转换的工作表才会有J2中的电子邮件地址。
wb。ExportAsFixedFormat
在检查电子邮件地址的If
块之外,因此,它将为每个工作表运行确定它是否应上移到最后一个结束if stmt之前?它应位于与检查电子邮件的if
匹配的结束if
之前它已修复!谢谢,我已经看了这么久了,我不知道它在if-stmt之外。当你有多个嵌套if语句时,你应该正确缩进你的代码,这使它更容易阅读和排除故障。