PDA

View Full Version : [SOLVED:] Using VBA to save a xlsm file (Excel 2013)



dmhzx
10-09-2017, 05:40 AM
I'm going round in circles here.
I have this code:


Set fdia = Application.FileDialog(msoFileDialogSaveAs)
With fdia
.AllowMultiSelect = False

.Title = "Select file name to save"
.InitialFileName = SavePath '(which ends with xlsm)
If .Show = False Then
MSG = "You've Cancelled the Save Operation. Entire Process will now be undone"
MsgBox MSG, vbCritical, "Save operation cancelled by user"
GoTo CleanExit:
End If
SavePath = .SelectedItems(1) 'Filedialog has changed both the extension and file type to xlsx

End With
wbDest.SaveAs SavePath, xlOpenXMLWorkbookMacroEnabled '(which errors -- see below)

Savepath going into the filedialog INCLUDES xlsm as the extension.
When the dialog opens WITH the initial filename it has xlsx as the extension. Automatically changed by MS .
I can't find anywhere at all how to set the extension (that is the proper file name) in the FileDialog.
As you might expect, I get an error when I try to save, because the xlsx workbook had code in it.

If I use GetSaveAsFileName, the IntitialFileName is compleltely ignored, and only the 'folder' part navigated to.

So far the only method I've found to get me a xlsm is to change the filename returned from the FileDialog
Is this a known bug: an unknown bug, another output from the MS irritants team or me missing something?

Looking at all the examples on the web, what I've coded SHOULD work, but it doesn't. - And I'm pretty certain used to work in 2007 and 2010.

dmhzx
10-09-2017, 06:53 AM
Well I've changed changed the Excel defaults to use type xlsm, and it STILL opens the FileDialog with my extension xlsm, changed to xlsx

mdmackillop
10-09-2017, 06:57 AM
See here (https://social.msdn.microsoft.com/Forums/office/en-US/c4f8cad5-90cc-4748-a05e-163f00b8903f/applicationfiledialogmsofiledialogsaveas?forum=exceldev)


Sub SaveTest()
With Application.FileDialog(msoFileDialogSaveAs)
.FilterIndex = 2
If .Show Then
ActiveWorkbook.SaveAs Filename:=.SelectedItems(1), _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
End With
End Sub

dmhzx
10-09-2017, 07:48 AM
Yes of course:
I read that earlier, but didn't notice that filter index was relevant. Now when I read it again thanks to your prompting, I see that the filter index tells the file dialog how many lines to skip before deciding what extension to put in (regardless of what you set it to in the first place).
So adding the extension to the file name is a waste of time. You really need to choose the file type with the filter index.
I did actually read the MS help on FilterIndex, and there wasn't even a hint that it was this that was controlling the file type (extension).
But thank you so much for the quick response.
I was missing something, but I fell it was rather obscure.
All is now well.