I've been trying to save some excel data as a custom text file format. By following this post I didn't got too far. Excel throws Run-time error '13': Type mismatch. Any ideas why this happens?
The error happens on third line:
Set dialog = Application.GetSaveAsFilename(FileFilter:="Custom file, *.custom")
Sub SaveCustomData()
Dim dialog As Variant
Set dialog = Application.GetSaveAsFilename(FileFilter:="Custom file, *.custom")
' !!! The code runs this far !!!
With dialog
.InitialFileName = Application.ActiveWorkbook.Path & Application.PathSeparator
.AllowMultiSelect = False
Dim file As String
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim fileStream As TextStream
If .Show = True Then
file = .SelectedItems(1)
Set fileStream = fso.CreateTextFile(dialog, ForWriting, True)
' Writing data to file here
fileStream.Close
End If
End With
End Sub
Application.GetSaveAsFilenamemethod returns asFalseif it's canceled, as per MSDN - hence producing the Type mismatch error (you can't open a boolean). Just create a simpleifcondition that handles this behaviour