运行时错误'1004':应用程序定义的错误或对象定义的错误

时间:2016-01-08 03:09:47

标签: excel vba

我通过这个错误阅读了一些现有的VBA问题,但我发现错误信息是一般的,并且有很多方法可以获得它。

我的VBA代码在下面,我试图找出为什么突然它以前不工作。 Excel突出显示的行位于2 *之间,我在代码中实际上没有:)

Sub publishPDF()

'
' PublishToPDF Macro
' Macro recorded 01/07/2016 by Pczarnota



' Export to PDF

    SaveFolder = "S:\DataOps\InvValidatedFeed\"
    DocName = Range("D1").Value

    *ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SaveFolder & DocName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False*


    MsgBox ("Another one down!")

End Sub

1 个答案:

答案 0 :(得分:2)

这必须与D1中的文件名有关,依赖于ActiveSheet property来定义D1的父工作表或非法文件名。

Sub publishPDF()
' PublishToPDF Macro
' Macro recorded 01/07/2016 by Pczarnota
' Export to PDF
    Dim saveFolder As String, docName As String

    saveFolder = "S:\DataOps\InvValidatedFeed\"   '<~~ access to the share or network drive?
    docName = Worksheets("Sheet1").Range("D1").Value  '<~~define the worksheet holding the filename!
    If CBool(InStr(1, docName, Chr(46))) Then 'check for a period (full stop)
        'remove it; the save type will add the appropriate one
        docName = Left(docName, InStr(1, docName, Chr(46)) - 1)
    End If

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveFolder & docName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    MsgBox ("Another one down!")

End Sub

这对我有用,但我没有复制网络共享,并在Sheet1中使用了 abc.xls !D1。