Excel批量导出到单个PDF"运行时错误1004"

时间:2018-03-07 02:49:47

标签: excel vba excel-vba

我有一个包含254个条目的Excel电子表格,我想将每行导出为单独的PDF。这段代码有效,我有两个电子表格。一个名为dataSheet,一个名为pdfSheet,它从dataSheet复制数据,将其粘贴到pdfSheet中,然后将pdfSheet导出为pdf。然后将该文件保存到文件夹中。

然而,我得到"运行时错误1004"每次都在第18次运行(它完美地工作)。我已经改组数据,以确保这不是一个问题,但我无法弄清楚为什么它会停止工作。我在调试器中逐步完成它,在第18个循环中我得到错误:

pdfSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="S:\Exports\" & "Export" & "\" & SignName & "\" & "Export.pdf"

以下是完整代码:

Sub ExportPDF()
Dim wholeBook As Workbook
Dim pdfSheet As Worksheet
Dim dataSheet As Worksheet
Dim path As String
Set pdfSheet = ThisWorkbook.Worksheets(2)
Set dataSheet = ThisWorkbook.Worksheets(1)
Set wholeBook = ThisWorkbook
path = wholeBook.FullName
Application.ScreenUpdating = False
For i = 2 To 254
    SignName = dataSheet.Cells(i, 4)
    Category = dataSheet.Cells(i, 3)
    DOB = dataSheet.Cells(i, 5)
    Address = dataSheet.Cells(i, 6)
    Phone = dataSheet.Cells(i, 7)
    Email = dataSheet.Cells(i, 8)
    Instagram = dataSheet.Cells(i, 9)
    Facebook = dataSheet.Cells(i, 10)
    Twitter = dataSheet.Cells(i, 11)
    ECD = dataSheet.Cells(i, 13)
    BOOTP = dataSheet.Cells(i, 14)
    WINRTTP = dataSheet.Cells(i, 15)
    PDOA = dataSheet.Cells(i, 16)
    AGOTP = dataSheet.Cells(i, 17)
    PATISU = dataSheet.Cells(i, 18)
    DACYP = dataSheet.Cells(i, 19)
    PRPFP = dataSheet.Cells(i, 20)
    GGAFTFO = dataSheet.Cells(i, 21)
    AROAFF = dataSheet.Cells(i, 22)
    BS = dataSheet.Cells(i, 23)
    UNISS = dataSheet.Cells(i, 24)
    WPSGA = dataSheet.Cells(i, 25)

    pdfSheet.Cells(3, 1).Value = SignName
    pdfSheet.Cells(5, 1).Value = Category
    pdfSheet.Cells(7, 1).Value = DOB
    pdfSheet.Cells(9, 1).Value = Address
    pdfSheet.Cells(11, 1).Value = Phone
    pdfSheet.Cells(13, 1).Value = Email
    pdfSheet.Cells(15, 1).Value = Instagram
    pdfSheet.Cells(17, 1).Value = Facebook
    pdfSheet.Cells(19, 1).Value = Twitter
    pdfSheet.Cells(21, 1).Value = ECD
    pdfSheet.Cells(23, 1).Value = BOOTP
    pdfSheet.Cells(25, 1).Value = WINRTTP
    pdfSheet.Cells(27, 1).Value = PDOA
    pdfSheet.Cells(29, 1).Value = AGOTP
    pdfSheet.Cells(31, 1).Value = PATISU
    pdfSheet.Cells(33, 1).Value = DACYP
    pdfSheet.Cells(35, 1).Value = PRPFP
    pdfSheet.Cells(37, 1).Value = GGAFTFO
    pdfSheet.Cells(39, 1).Value = AROAFF
    pdfSheet.Cells(41, 1).Value = BS
    pdfSheet.Cells(43, 1).Value = UNISS
    pdfSheet.Cells(44, 1).Value = WPSGA
    If Len(Dir("S:\Exports\" & "Export" & "\" & SignName, vbDirectory)) = 0 Then
        MkDir "S:\Exports\" & "Export" & "\" & SignName
    End If
    pdfSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="S:\Exports\" & "Export" & "\" & SignName & "\" & "Export.pdf"
    ThisWorkbook.Save
    Application.Workbooks.Open (ThisWorkbook.FullName)
    Next i
Application.ScreenUpdating = True
End Sub

完整错误是

Run-time-error '1004':
Application-defnied or object-defined error

0 个答案:

没有答案