Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Word - Save As dialog with initial filename

Tags:

ms-word

vba

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?

like image 885
outofmind Avatar asked Oct 06 '15 09:10

outofmind


1 Answers

Have you tried replacing the Call ActiveDocument.SaveAs line with

Application.FileDialog(msoFileDialogSaveAs).Execute
like image 138
Stanley Avatar answered Oct 03 '22 22:10

Stanley