I have an Excel linked Word document with VBA code in Excel. At the end of the code sequence I want to save the Word document in a specified folder and with file format "Word document" (aka doc or docx) and afterwards as PDF document.
I am trying to do so via, but the output file I get does not have "any" format. Also specifying the file format in the filename does not help.
Additionally, trying to export the file as PDF does not work either (invalid procedure ...)
Any suggestions?
Best, Franzi
Sub WordErstellen(rechnungsnummer As Variant, firma As Variant, name As Variant, datum As Variant)
Dim WordApp As Object
Dim dateiname, pfad As Variant
path_word = "C:\mypath\Word\"
path_pdf = "C:\mypath\PDF\"
myfilename = rechnungsnummer & "_" & firma & "_" & name & "_" & datum
Set WordApp = CreateObject("Word.Application")
Set doc = WordApp.Documents.Add("C:\mypath\mytemplate.docm")
WordApp.Visible = True
doc.Activate
'Save
doc.SaveAs2 Filename:=path_word & myfilename, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
doc.ExportAsFixedFormat OutputFileName:=path_pdf & myfilename, _
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
End Sub
I am trying to do so via, but the output file I get does not have "any" format. Also specifying the file format in the filename does not help.
Additionally, trying to export the file as PDF does not work either (invalid procedure ...)
wdExportFormatPDF
,wdExportAllDocument
,wdExportDocumentContent
andwdExportCreateNoBookmarks
. Open MS Word and in the VBE, in the immediate window, type for example?wdExportFormatPDF
to get it's value. Do the same for others – Siddharth RoutwdExportFormatPDF
is17
. So you declareConst wdExportFormatPDF As Integer = 17
at the very top. Do the same for others. AddOption Explicit
on the very top ;) – Siddharth RoutOption Explicit
at the top of your code modules will alert you to this sort of problem (undefined variables) by raising a compile-time error. – David Zemens