【发布时间】:2016-06-21 19:53:41
【问题描述】:
我有一个电子表格,其中包含直到最近才有效的表单和宏。我第一次看到所描述的行为是我第一次尝试在 Excel 2016 (Windows 10) 中使用此特定功能,但我不确定这是不是原因。
我遇到的问题是以下行
Worksheets("Label Template - 100X150").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfFilePath, Quality:=xlQualityMinimum, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
来自以下宏:
Sub PDFLabelsSheet()
'On Error GoTo errHandler
'enter name and select folder for file
strFile = "Labels_PrintGroup-" & lstPrintGroup.Value _
& "_" _
& Format(Now(), "yyyy-mm-dd\_hhmm") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
Worksheets("Label Template - 100X150").Visible = True
UnprotectTab "Label Template - 100X150"
pdfFilePath = Application.GetSaveAsFilename(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If pdfFilePath <> "False" Then
Worksheets("Label Template - 100X150").Select
Worksheets("Label Template - 100X150").Range("A1").Select
Worksheets("Label Template - 100X150").Range("A1").Activate
Cells.Activate
Worksheets("Label Template - 100X150").PageSetup.FirstPageNumber = 1
Worksheets("Label Template - 100X150").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfFilePath, Quality:=xlQualityMinimum, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
exitHandler:
ExecutionEnd
Exit Sub
errHandler:
MsgBox "Something went wrong, a PDF could not be created", vbCritical
Resume exitHandler
End Sub
我尝试了 ExportAsFixedFormat 函数的排列,将其更改为 Worksheets("name")、(number)、ActiveSheet....等。
在执行此特定 Sub 之前,工作表是未隐藏且未受保护的。我尝试将整个范围设置为 PrintArea,我也尝试选择并激活要导出的范围。
最令人沮丧的部分是它有时会起作用。我已经尽可能地放入代码来吐出 PageSetup 集合的每个属性,以确保某些东西不会炸毁东西,因为输出如此严格地依赖于页面设置。
当我手动导航到此工作表并从文件菜单中选择导出功能时,我得到了同样的错误。如果我在该工作表中单击一下,导出功能以及宏工作。
PDFLabelSheets Sub 确实从另一个 Sub 调用(由按下表单的按钮调用),它将大量数据编译到此选项卡中,尽管我已将其排除为罪魁祸首,因为仅 PDF 代码的连续运行将成功然后在不运行其他代码的情况下失败。
【问题讨论】:
-
为什么你认为
ExportAsFixedFormat行导致了错误?? -
单步执行代码,这就是挂断的那一行。我已将 Subs 减少为仅包含该行,但它失败了
-
虚幻。我刚刚在 Excel 中将视图从页面布局/打印更改为正常,我再也无法让它失败。我已经进行了全方位的测试。为什么,微软,为什么?