VBA Word - Save as a dialog with the initial file name

I have a vba macro that makes some changes to the current document and determines the file name that should be used for it - if the document is not saved as this file name, but the user should be prompted to do this (but should be able to change the default setting) .

I found two possibilities that are both not ideal (I will need a combination of the two).

First approach:

Application.Dialogs(wdDialogFileSaveAs).Show 

Opens the Save As dialog and allows you to change the format and file name, but the default file name is the old file name or title (before the first special character, such as empty or - ) of the document (if it has not been saved yet - change the name of the document helps a little, as the proposed file name will contain - ). Can I change the initial file name displayed in the Save As dialog box?

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 

FileDialog will only select the file name, so we must explicitly save it. This approach will show the filename I want, but if the user changes the suffix, for example .pdf , the file will be saved in .docx format (using the suffix .pdf). I did not plan to have a huge difference here in rare cases when 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?

+5
source share
1 answer

You tried to replace the Call ActiveDocument.SaveAs line with

 Application.FileDialog(msoFileDialogSaveAs).Execute 
+6
source

Source: https://habr.com/ru/post/1233035/


All Articles