Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word VBA SaveAs error: incompatible file type and file extension

Tags:

ms-word

vba

I'm trying to do a save-as with the following code in Word 2016:

Dim doc As Word.Document
Dim fpFile as string

Set doc = ActiveDocument
fpFile = doc.Path & "\" & doc.Name

doc.SaveAs Filename:=Left(fpFile, Len(fpFile) - 4) & "docx", FileFormat:=wdFormatDocument 

My Filename argument using the Left and Len functions evaluates to:

C:\Users\My Name\Documents\My Company\My Client\test folder\HeaderTemplate.docx

This looks fine to me but I'm getting an error 6294: 'incompatible file type and file extension' on the SaveAs line.

I've tried putting the Filename to a string variable first and I've tried the SaveAs2 method, both with the same results. I do want to save as a standard non-macro-enabled docx file.

Any ideas?

Thanks.

like image 435
Absinthe Avatar asked Dec 11 '22 15:12

Absinthe


1 Answers

wdFormatDocument is .doc,

wdFormatXMLDocument is .docx

From MSDN (for Office 2007):

Name                         Value Description
wdFormatDocument                 0 Microsoft Office Word format.
wdFormatDOSText                  4 Microsoft DOS text format.
wdFormatDOSTextLineBreaks        5 Microsoft DOS text with line breaks preserved.
wdFormatEncodedText              7 Encoded text format.
wdFormatFilteredHTML            10 Filtered HTML format.
wdFormatHTML                     8  Standard HTML format.
wdFormatRTF                      6 Rich text format (RTF).
wdFormatTemplate                 1 Word template format.
wdFormatText                     2 Microsoft Windows text format.
wdFormatTextLineBreaks           3 Windows text format with line breaks preserved.
wdFormatUnicodeText              7 Unicode text format.
wdFormatWebArchive               9 Web archive format.
wdFormatXML                     11 Extensible Markup Language (XML) format.
wdFormatDocument97               0 Microsoft Word 97 document format.
wdFormatDocumentDefault         16 Word default document file format. For Microsoft Office Word 2007, this is the DOCX format.
wdFormatPDF                     17 PDF format.
wdFormatTemplate97               1 Word 97 template format.
wdFormatXMLDocument             12 XML document format.
wdFormatXMLDocumentMacroEnabled 13 XML document format with macros enabled.
wdFormatXMLTemplate             14 XML template format.
wdFormatXMLTemplateMacroEnabled 15 XML template format with macros enabled.
wdFormatXPS                     18 XPS format.
like image 153
arcadeprecinct Avatar answered May 29 '23 18:05

arcadeprecinct