I've been working on this Save As macro and I'm getting stuck on how to exit a sub when the user clicks cancel on the Save As dialog box.
I looked through many forums and can't find a solution to how I'm naming and saving the files with the macro. Any ideas?
Private Sub PTR_Btn_Click() 'Select All ActiveLayer.Shapes.All.CreateSelection Dim sFolderPath As String Dim sFileName As String 'Set your folder here sFolderPath = "D:\Sync\Sales Team\" & Me.Sales.Value & "\" 'Open file save as dialog, you can change this to *.* to see all files sFileName = CorelScriptTools.GetFileBox("CorelDRAW Files (*.cdr)|*.cdr", "Save As", 1, frmMain.txtAct.Value & "_" & "PTR" & "_" & Round(ActivePage.SizeWidth, 3) & "x" & Round(ActivePage.SizeHeight, 3) & "_" & Format(Now, "mmddyy"), , sFolderPath) If sFolderPath = "False" Then Exit Sub End If ActiveDocument.SaveAs sFileName GMSManager.RunMacro "JQ_Quick_Export", "JQ.Toggle_Quick_Export" End Sub
Maybe I'm missing something here, but wouldn't you want to be looking at what CorelScriptTools.GetFileBox has returned (empty string?) in order to determine whether the user has canceled?
The code you posted is checking sFolderPath, not sFileName, and you are comparing to "False", not to "".
I've tried sFileName and still runs through the code when I cancel. From my understanding, this is returning a string for the file name.Is the code = "False" checking if there's a file name? There's always a file name since the macro is creating it. I don't know how to check if the user canceled if there's already a file name in the dialog. I tried deleting the file in the save as dialog and then canceled, but that ran through the rest of the code as well. Also Tried = " " still no luck.
If you want to "Save As", and you already know the exact filepath and filename you want to use, then you don't need to use CorelScriptTools.GetFileBox at all. That's not an integral part of the "Save As" process; it's a way to allow the user to browse to select a folder, and to manually type in whatever filename they want.
Do you want the user to have flexibility there to select a different folder, and/or specify a different filename?
CorelScriptTools.GetFileBox returns a string that shows you what filepath and filename the user came up with. If they cancel, then I think it should return an empty string - and that's exactly what I check for when I use it in my macros (e.g., my Quick Export).
Well, I'm picking the salesman's folder from the ComboBox, but I have to manually select the right account folder. Unless there's a way to look at the account number from my form and match it to the correct account folder containing that number, but I haven't gotten that far... I think I got it working. You were right, it was returning an empty string. For the saving part, I told it to look for the sign type in the file name and then save.
Private Sub PTR_Btn_Click() 'Select All ActiveLayer.Shapes.All.CreateSelection Dim sFolderPath As String Dim sFileName As String 'Set your folder here sFolderPath = "D:\Sync\Sales Team\" & Me.Sales.Value & "\" 'Open file save as dialog, you can change this to *.* to see all files sFileName = CorelScriptTools.GetFileBox("CorelDRAW Files (*.cdr)|*.cdr", "Save As", 1, frmMain.txtAct.Value & "_" & "PTR" & "_" & Round(ActivePage.SizeWidth, 3) & "x" & Round(ActivePage.SizeHeight, 3) & "_" & Format(Now, "mmddyy"), , sFolderPath) 'User Canceled If sFileName = " " Then Exit Sub End If 'If sign type is in the file, name then save If sFileName Like "*PTR*" Then ActiveDocument.SaveAs sFileName GMSManager.RunMacro "JQ_Quick_Export", "JQ.Toggle_Quick_Export" End If End Sub
(My reply was marked as spam. I'll try again...)
Well, I'm picking the salesman's folder from the ComboBox, but I have to manually select the right account folder. Unless there's a way to look at the account number from my form and match it to the correct account folder containing that number, but I haven't gotten that far...
I think I got it working. You were right, it was returning an empty string. For the saving part, I told it to look for the sign type in the file name and then save.