0
votes

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 ...)

1
If you are latebinding then you will have to define the constants wdExportFormatPDF, wdExportAllDocument, wdExportDocumentContent and wdExportCreateNoBookmarks. Open MS Word and in the VBE, in the immediate window, type for example ?wdExportFormatPDF to get it's value. Do the same for othersSiddharth Rout
For example the value of wdExportFormatPDF is 17. So you declare Const wdExportFormatPDF As Integer = 17 at the very top. Do the same for others. Add Option Explicit on the very top ;)Siddharth Rout
NB: using Option 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

1 Answers

0
votes

Try:

doc.SaveAs2 path_word & myfilename & ".docx", 12, False, "", True
doc.SaveAs2 path_word & myfilename & ".pdf", 17, False, "", True

PS: you don't need:

doc.Activate