Andy Fish said:
BTW when I tried it with the macro recorder I actually got the value 100
which is the value I have used.
With external converters (which is what the docx converter is implemented
as) the number to use for the SaveAs will vary from PC to PC depending on
what other converters are also installed.
If you want to have code which will correctly Save As docx on any PC, then
you need to dynamically find the number to use for the SaveAs command. This
can be found by reading the SaveFormat property of the appropriate
FileConverter object.
The following macro (adapted from one in the VBA Help) lists all the file
converters on your PC, give the class name, friently name, and the
SaveFormat number.
Sub FileConverterList()
Dim cnvFile As FileConverter
Dim docNew As Document
'Create a new document and set a tab stop
Set docNew = Documents.Add
docNew.Paragraphs.Format.TabStops.Add _
Position:=InchesToPoints(3)
'List all the converters in the FileConverters collection
With docNew.Content
.InsertAfter "Class" & vbTab & "Name" & vbTab & "Number"
.InsertParagraphAfter
For Each cnvFile In FileConverters
If cnvFile.CanSave = True Then
.InsertAfter cnvFile& vbTab & cnvFile.FormatName & vbTab & _
cnvFile.SaveFormat
.InsertParagraphAfter
End If
Next
.ConvertToTable
End With
End Sub
From running this, it is possible to deduce that the class name we are
interested in is "Word12"
Therefore, in order reliably get the save format number, use this function
Function GetSaveFormat(sClassname As String) As Long
Dim cnvFile As FileConverter
For Each cnvFile In FileConverters
If cnvFile.ClassName = sClassname Then
If cnvFile.CanSave = True Then
GetSaveFormat = cnvFile.SaveFormat
Exit Function
End If
End If
Next cnvFile
End Function
To use the function to save a document as docx, proceed as follows
Dim iSaveFormat As Long
iSaveFormat = GetSaveFormat("Word12")
If iSaveFormat = 0 Then
MsgBox "Word 2007 file converter is not installed"
Else
ActiveDocument.SaveAs FileName:="filename.docx", FileFormat:=iSaveFormat
End If