尝试将“GetFullNamePDF()”传递给Filename属性,但收到以下错误:“编译错误:预期的结束子”
Sub PrintPDF()
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
我对VBA一无所知,从question I asked yesterday获得了上述代码,但当时无法测试。猜测错误与函数有关,因为代码在没有添加函数和文件路径/名称硬编码的情况下工作。
代码的想法是动态使用自身的文件名来命名PDF的路径和文件。如果您有任何问题,请发表评论 - 谢谢!
答案 0 :(得分:6)
您无法在过程中嵌套函数。你需要将它移到上面:
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
'This should be
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
'Remove the quotes from GetFullNamePDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
GetFullNamePDF(), Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
答案 1 :(得分:1)
在子中声明函数是非法的。 它应该是这样的:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
答案 2 :(得分:1)
像这样:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
Dim sFileName As Variable
sFileName=GetFullNamePDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sFilename, Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub