I have a vba macro that makes some changes to the current document and determines a filename that should be used for it - if the document isn't saved as that filename yet the user should be prompted to do so (but should be able to alter the default setting).
I found two possibilities that both are not perfect (I'd need a mix of those two).
First approach:
Application.Dialogs(wdDialogFileSaveAs).Show
Opens the Save As
dialog and lets you change the format and name of the file, but the default file name is the old filename or the title (up to the first special character like blank or -
) of the document (in case it wasn't saved yet - changing the title of the document is of little help as the suggested filename will contain -
). Is it possible to change the initial filename shown in the Save As
dialog?
Second approach:
Application.FileDialog(msoFileDialogSaveAs).InitialFileName = filename
Dim choice As Integer
choice = Application.FileDialog(msoFileDialogSaveAs).Show
If choice <> 0 Then
filename = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
Call ActiveDocument.SaveAs(filename:=filename, FileFormat:=wdFormatDocumentDefault)
End If
The FileDialog
will choose a filename only, so we have to save it explicitely.
This approach will show the filename
I want, but if the user changes the suffix to e.g .pdf
the file will still be saved in the .docx
format (using the suffix .pdf). I didn't plan to have huge distinction of cases here for the rare case the user needs a different format than .docx
. Is there an easy way to save the file in the correct format using this second approach?
Have you tried replacing the Call ActiveDocument.SaveAs line with
Application.FileDialog(msoFileDialogSaveAs).Execute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With