VBA: Prefill SaveAs Dialog Box With Defined Name, Give Alert If Filename Exists In Directory

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello [Merry Christmas]
I have this code and I need some adjustments:
1. Fill the name field with predefined name
- "My File" + current year
Which is to say that if I run the code right now, the name field should display "My File 2021"
2. When I click the Save button, I want to scan the directory where I am saving the file for the availability of same name as in the name field. And if I find a match, I want to flag a message alert.


Code:
Sub SaveFileCode
ChDrive ThisWorkbook.Path 
ChDir ThisWorkbook.Path 

FileSaveAs = Application.GetSaveAsFilename(FileFilter:="Exel Files (*.xlsm),*.xlsm",Title:="Add Name")

If FileSaveAs = False Then 
        MsgBox "Operation terminated"
Else
       Application.EnableEvents=False 
       ThisWorkbook.SaveAs FileName:=FileSaveAs, FileFormat:=52, Password:="", writerespassword:="", ReadOnlyRecommended:=False 
       Application.EnableEvents=True
End Sub

Can someone please help me with it?

Thanks in advance.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Simple way to check is to use the Dir() function.

VBA Code:
Sub SaveFileCode
ChDrive ThisWorkbook.Path 
ChDir ThisWorkbook.Path 
Dim MyFile as String 

   MyFile = "xxxxx" ' Here you have to provide the Path and Filename

FileSaveAs = Application.GetSaveAsFilename(FileFilter:="Exel Files (*.xlsm),*.xlsm",Title:="Add Name")

If FileSaveAs = False Then 
        MsgBox "Operation terminated"
Else
       '=============================
       '   Check whether file exists
       '   You will need a variable to contain the:
       '      path and filename 
       '=============================
       If Dir(MyFile) = "" then
            '=============================
            '   File does not exist, so proceed to save
            '=============================
            Application.EnableEvents=False 
            ThisWorkbook.SaveAs FileName:=FileSaveAs, FileFormat:=52, Password:="", writerespassword:="", ReadOnlyRecommended:=False 
            Application.EnableEvents=True
      Else
           '======================
           '   File exists; use another name
           '======================
           Application.StatusBar = "File already exists! Please use another name!"
           Exit Sub
      End If

End Sub
 
Upvote 0
Okay great!

So instead of using a new variable "MyFile",

I used the Dir () function as :

Code:
If Dir (FileSaveAs) = "" Then
.................
.................
End If

So that part of my request (option #2) is resolved.

Now I am left with the option #1:

How to get the dialog box to open with a predefined name instead of the original name of the workbook.

Once again, thank you for your code @Larry Haydn
 
Upvote 0
Cross posted at:

Reason:
I still need help with the request #1 as described in post #1 above.
 
Upvote 0
I still need help with the request #1 as described in post #1 above.

Change:
VBA Code:
FileSaveAs = Application.GetSaveAsFilename(FileFilter:="Exel Files (*.xlsm),*.xlsm",Title:="Add Name")

to:

VBA Code:
DefaultFileName = "My File " & Year(Date)
FileSaveAs = Application.GetSaveAsFilename(InitialFileName:=DefaultFileName, FileFilter:="Exel Files (*.xlsm),*.xlsm", Title:="Add Name")
 
Upvote 0
Solution

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top