VBA错误:"编译错误:预期的结束子"

blu*_*ers 2 excel vba

尝试将"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
Run Code Online (Sandbox Code Playgroud)

我对VBA一无所知,从我昨天问的一个问题中得到了上面的代码,但当时无法测试.猜测错误与函数有关,因为代码在没有添加函数和文件路径/名称硬编码的情况下工作.

代码的想法是动态使用自身的文件名来命名PDF的路径和文件.如果您有任何疑问,请发表评论 - 谢谢!

kgi*_*kis 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
Run Code Online (Sandbox Code Playgroud)