VBA to display Save As Dialog Box

srdavisgb

Board Regular
Joined
Nov 5, 2011
Messages
51
Thank you in advance for your assistance.

I have written a macro to copy a worksheet to a new workbook. I have identified a file name and I prompt the user to password protect the file. It appears the company blocks some users from saving to the C:\Temp\ drive. I think they add the User ID in the path (e.g., C:\Temp\KZ4XWS\). To avoid this issue, I would like to prompt the user with the Save As dialog box so they can select a save location.

How to I prompt for a Save Dialog box for location and populate the file name and password?


Below is my code:

Dim ESC_Doc_Name As String
Dim Password As String
Dim ErrorNumber As Integer

ESC_Doc_Name = Sheets(1).Cells(5, 2)
Password = Sheets(1).Cells(9, 2)

'Check to ensure a valid sheet name is entered
If ESC_Doc_Name = "" Or ErrorNumber = "13" Then
MsgBox "You must enter the Employee's name exactly as it is displayed on the tab in this Workbook. Please enter the Employee's name.", , "Enter ECS Name Reminder"
Exit Sub
End If

'Check to ensure a "PASSWORD" is entered
If Password = "" Or ErrorNumber = "13" Then
MsgBox "You must enter a password to create this file. Please enter a password of up to 8 characters in length.", , "Enter Password Reminder"
Exit Sub
End If


Sheets(ESC_Doc_Name).Select
Sheets(ESC_Doc_Name).Copy
Application.Dialogs(xlDialogSaveAs).Show
'ActiveWorkbook.SaveAs Filename:="C:\Temp\" & ESC_Doc_Name & "_ECS.xlsx", FileFormat _
':=xlOpenXMLWorkbook, Password:=Password, WriteResPassword:="", ReadOnlyRecommended:=False _
', CreateBackup:=False


'ActiveWorkbook.SaveAs Filename:="C:\Temp\" & ESC_Doc_Name & "_ECS.xlsx", FileFormat:= _
' :=xlOpenXMLWorkbook, Password:=Password, WriteResPassword:="", ReadOnlyRecommended:=False _
' , CreateBackup:=False
ActiveWorkbook.Close


Sheets("Macro").Select
Range("B5").ClearContents
Range("B9").ClearContents
ActiveWindow.SelectedSheets.Visible = False

MsgBox "Your new file has been saved to C:\TEMP\ directory.", , "File Location"


End Sub


Again, thanks for your assistance.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
This line will open the SaveAs dialog box.

Application.Dialogs(xlDialogSaveAs).Show


If you have already named the activeworkbook, it's name will populate the box. The dialog box accepts several arguments:
document_text, type_num, prot_pwd, backup, write_res_pwd, read_only_rec
 
Upvote 0
I guess I don't know how to name the activeworkbook without saving it. Currently, my code saves the workbook and then displays the xlDialogSaveAs dialog box. How do I name the workbook to get the filename pre-populated in the dialog box. Below is my code:

ActiveWorkbook.SaveAs Filename:=ESC_Doc_Name & "_ECS.xlsx", FileFormat _
:=xlOpenXMLWorkbook, Password:=Lname & "2015", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.Dialogs(xlDialogSaveAs).Show

Application.Dialogs(xlDialogSaveAs).Show

'ActiveWorkbook.SaveAs Filename:="C:\Temp\" & ESC_Doc_Name & "_ECS.xlsx", FileFormat:= _
' :=xlOpenXMLWorkbook, Password:=Password, WriteResPassword:="", ReadOnlyRecommended:=False _
' , CreateBackup:=False

ActiveWorkbook.Close

Again, thanks for your help.
 
Upvote 0
Since you have already saved the file before you open the SaveAs dialog box, the file name should populate the box.
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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