2010-12-17 74 views
1

试图通过“GetFullNamePDF()”的文件名属性,但得到以下错误:“编译错误:预期结束子”VBA错误:“编译错误:预期结束子”

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的路径和文件。如果您有任何问题,请点评 - 谢谢!

回答

3

不能嵌套过程中的功能。你需要将它移动到上面:

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 
+0

感谢您花时间阅读代码并确保一切正确;明智的我的代码说我的代码更好,但从未使用Excel的VBA。 – blunders 2010-12-17 15:47:39

+0

工程,只是测试它,谢谢! – blunders 2010-12-17 15:53:43

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 
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 
+0

@Remou:在代码中没有提及“sFilename”,不要认为这会起作用。 – blunders 2010-12-17 16:01:06

+0

@Blunder你忘了我写了这个函数吗?我非常非常熟悉VBA(检查VBA标签上的用户)。将变量设置为函数的结果并不罕见,这使得调试变得更加容易。在你重写函数时有一个错误,我会纠正它。 – Fionnuala 2010-12-17 16:10:05

+0

@Remou:你是对的,它会工作 - 将一个变量设置为函数结果的原因是什么? – blunders 2010-12-17 16:16:54