简体   繁体   English

将Word文档另存为pdf时在Excel中出现运行时错误5

[英]Run-time error 5 in excel while saving a word document as a pdf

So I've been running this code on a couple computers for awhile. 因此,我已经在几台计算机上运行了一段时间。 However, the spreadsheet has begun to crash and refuses to save, so I created a new one, with everything the same. 但是,电子表格已经开始崩溃,并且拒绝保存,因此我创建了一个新的电子表格,所有内容都相同。 It crashes as I try and save my word document as a PDF, specifically, this line 当我尝试将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

Here is my full code, My apologies for lack of comments etc, it was written to be used only by me. 这是我的完整代码,对于缺乏评论等,我深表歉意,仅写给我使用。

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 结束子

Solved it. 解决了。

Needed to include the Microsoft Word 14.0 Object Library 需要包括Microsoft Word 14.0对象库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM