I have an excel calculation which contains information for a Word Document. What I want is to open the word document and save it as an pdf automatically - with a macro in Excel.
I already tried the following:
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
.Documents.Open (LocationTemplate)
.ExportAsFixedFormat OutputFileName:= _
OfferPath, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ChangeFileOpenDirectory _
DestinationPath
.Quit
End With
What is the mistake? Looking forward to your support.
it seems that youre not selecting the open document
try something like this
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
.Documents.Open (LocationTemplate)
.Activate
ActiveDocument.ExportAsFixedFormat
.ExportAsFixedFormat OutputFileName:= _
OfferPath, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ChangeFileOpenDirectory _
DestinationPath
.Quit
End With
Good Luck
Your problem stems from the fact that in the With
block you are referring to WordApp.Application
(which in itself is redundant and could be reduced to WordApp
as it already represents a Word.Application
object) and thus with the line .ExportAsFixedFormat [...]
you are essentially doing:
Word.Application.ExportAsFixedFormat
This method doesn't exist on the Application
object. (Now read the error description again - notice anything?)
If you instead of late-binding the Word.Application
object via CreateObject()
set a reference to the Word Object Model (Menu: Extras - References) you can do things like:
Dim wordApp As Word.Application
Set wordApp = New Word.Application
With wordApp.Documents.Open LocationTemplate
.ExportAsFixedFormat [...]
End With
Which provides you with the (much needed) intellisense, as well as compile-time errors instead of runtime errors when you try to call wrong methods.
The mistake is that you execute .ExportAsFixedFormat
on the Word.Application object. This method is valid for a Word document . Your code should be more like what follows.
Note that I've added the variable declarations for WordApp
and WordDoc
and also code to release these objects.
Dim WordApp as Object
Dim WordDoc as Object
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
Set WordDoc = .Documents.Open (LocationTemplate)
WordDoc.ExportAsFixedFormat OutputFileName:= _
OfferPath, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
.Quit 0 'To not save changes to the document, just close it
Set WordDoc = Nothing
End With
Set WordApp = Nothing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.