【发布时间】:2020-12-13 08:30:59
【问题描述】:
我有以下 vba 代码
Sub SaveAsPDF()
Dim FileAndLocation As Variant
Dim strPathLocation As String
Dim strFilename As String
Dim strPathFile As String
strPathLocation = Worksheets("Sheet1").Range("A2").Value
strFilename = Worksheets("Sheet1").Range("A4").Value
strPathFile = strPathLocation & strFilename
Sheets("Sheet2").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strPathLocation & strFilename & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
我想要实现的是将Sheet2作为pdf保存到指定的路径和指定的文件名
【问题讨论】:
-
有什么问题/错误?
strPathLocation的末尾有路径分隔符吗? -
我遇到运行时错误,当我将 Sheet2 调试到 vba 末尾时以黄色突出显示
-
错误信息是?
-
确保
Debug.Print strPathLocation & strFilename & "pdf"- 很可能它缺少路径分隔符。 -
您没有包含
fPath- 您只有FileName:=fName。最有可能应该是FileName:=fPath & Application.PathSeparator & fName。