clif
03-18-2014, 12:40 AM
There is automation error in the following code.
How to solve it
Sub Transfer()
Dim wb As Workbook
Dim ws As Worksheet
Dim wrdApp As Word.Application
Dim wrdDoc As Document
'assign object values
Set wb = ThisWorkbook
Set ws = wb.Sheets("Transfer")
Set wrdApp = CreateObject("Word.Application")
Dim a As Integer
For a = 2 To 4
ws.Activate
Set wrdDoc = wrdApp.Documents.Open(ws.Cells(a, 2).Value)
wrdApp.Visible = False
wrdDoc.Activate
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ws.Cells(a, 4).Value, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
wrdDoc.Close
wrdApp.Quit
Next a
Set wrdDoc = Nothing
Set wrdApp = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub
How to solve it
Sub Transfer()
Dim wb As Workbook
Dim ws As Worksheet
Dim wrdApp As Word.Application
Dim wrdDoc As Document
'assign object values
Set wb = ThisWorkbook
Set ws = wb.Sheets("Transfer")
Set wrdApp = CreateObject("Word.Application")
Dim a As Integer
For a = 2 To 4
ws.Activate
Set wrdDoc = wrdApp.Documents.Open(ws.Cells(a, 2).Value)
wrdApp.Visible = False
wrdDoc.Activate
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ws.Cells(a, 4).Value, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
wrdDoc.Close
wrdApp.Quit
Next a
Set wrdDoc = Nothing
Set wrdApp = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub