2014-09-01 74 views
0

所以我一直在一些計算機上運行此代碼一段時間。但是,電子表格已經開始崩潰並且拒絕保存,所以我創建了一個新的電子表格,並且所有內容都一樣。它崩潰,我試着和我的Word文檔保存爲PDF,具體而言,此行在Excel中運行時錯誤5,同時將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 

結束子

回答

0

解決它。

需要包含Microsoft Word 14.0對象庫