将word文档另存为pdf时,excel中的运行时错误5

时间:2014-09-01 12:29:45

标签: excel-vba vba excel

所以我一直在几台计算机上运行这段代码。然而,电子表格已经开始崩溃并拒绝保存,所以我创建了一个新的,一切都相同。当我尝试将我的word文档保存为PDF时崩溃,特别是这行

            wrdDoc.ExportAsFixedFormat OutputFileName:=Path & "Lot " & Lot_Number & " " & Address & " " & Suburb & " " & Job & " - s40" & ".pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
            OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

这是我的完整代码,我对缺乏评论等道歉,它只是由我使用。

Sub AutoFill()

ScreenUpdating = False

Dim Job As String
Dim Rail As String
Dim Panel_Type As String
Dim Address As String
Dim Lot_Number As Integer
Dim Suburb As String
Dim Town As String
Dim Town_Check As String
Dim Current_Date As String
Dim DTC As String
Dim WordFileName As String
Dim Path As String
Dim i As Integer
Dim wrdApp As Object
Dim wrdDoc As Object

Dim count As Integer
count = Range("Solarcount")


Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    wrdApp.DisplayAlerts = wdAlertsNone

For i = 1 To count
    Job = Range("WordArray").Cells(i, 1)
    Rail = Range("WordArray").Cells(i, 2)
    Panel_Type = Range("WordArray").Cells(i, 3)
    Lot_Number = Range("WordArray").Cells(i, 4)
    Suburb = Range("WordArray").Cells(i, 7)
    Address = Range("WordArray").Cells(i, 11)
    Town = Range("WordArray").Cells(i, 10)
    Town_Check = Range("WordArray").Cells(i, 12)
    Current_Date = Range("WordArray").Cells(i, 14)
    DTC = Range("WordArray").Cells(i, 15)

    Path = Range("Path")

    Select Case Rail
        Case "Blue Sun"
            WordFileName = Range("FileNames").Cells(1, 1)
        Case "Clenergy"
            WordFileName = Range("FileNames").Cells(2, 1)
        Case "Conergy"
            WordFileName = Range("FileNames").Cells(3, 1)
        Case "Sunlock"
            WordFileName = Range("FileNames").Cells(4, 1)
    End Select

    Set wrdDoc = wrdApp.Documents.Open(Path & WordFileName, , True)
    With wrdDoc
        With .Bookmarks
            .Item("Address").Range = Address
            .Item("Current_date").Range = Current_Date
            .Item("Job_1").Range = Job
            .Item("Job_2").Range = Job
            .Item("Lot_Number").Range = Lot_Number
            .Item("Panel_Type").Range = Panel_Type
            .Item("Panel_Type_2").Range = Panel_Type
            .Item("Suburb").Range = Suburb
            .Item("Town").Range = Town
            .Item("Town_check").Range = Town_Check
            If Customer = "Sunlock" Then
                .Item("DTC").Range = DTC
            End If
        End With

        wrdDoc.SaveAs (Path & "Lot " & Lot_Number & " " & Address & " " & Suburb & " " & Job & " - s40" & ".doc")
        wrdDoc.ExportAsFixedFormat OutputFileName:=Path & "Lot " & Lot_Number & " " & Address & " " & Suburb & " " & Job & " - s40" & ".pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
            OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

        .Close ' close the document
    End With
Next

wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing

ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

解决了它。

需要包含Microsoft Word 14.0对象库