Force the confirmation question for saving

Solved
Ramon1 Posted messages 364 Registration date   Status Member Last intervention   -  
Ramon1 Posted messages 364 Registration date   Status Member Last intervention   -
Bonjour,

I have recorded the triggering of a macro in the Task Scheduler that runs the update from a macro "Deadline" on the 3rd of every month.
I also generate the recording of my file on C:\temp and a backup on E:\.
However, I want the user to not be prompted during the macro, so that when the box appears: Confirm save: The file already exists, do you want to replace it, "yes" or "no", it is forced to YES.
I found 2 lines of code that seem to meet my needs, but it works for my first save on C:\, while for my backup on E:\, the save does not take place!!!

I specify that I am quite a beginner and that I often use the macro recorder and inspiration from forums, but here I do not see my mistake.
Thank you for your help

Here is the code:

Private Sub Workbook_Open() Sheets("Deadline").Select 'Sheet to select If Range("E1").Value = Date Then Call Deadline 'Macro to execute End If Sheets("Accounting").Select End Sub Sub Save_C_and_E() ' ' Save_C_and_E Macro Sheets("Accounting").Select Range("A3").Select Sheets("Deadline").Select ActiveSheet.Unprotect Password:="1447" Range("A1").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 13434879 .TintAndShade = 0 .PatternTintAndShade = 0 End With ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="1447" Sheets("Accounting").Select Range("A4").Select Range("A4").End(xlDown).Offset(1, 0).Select ActiveWorkbook.Saved = True ChDir "C:\temp" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="Accounting_Raymond.xlsm" ' Overwrite existing file.xls' Application.DisplayAlerts = True 'Reset absolutely afterwards ChDir "E:\A_Perso_10.09.19\Bank\Accounting" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="Accounting_Raymond.xlsm" ' Overwrite existing file.xls' Application.DisplayAlerts = True 'Reset absolutely afterwards Application.Quit End Sub


Configuration: Windows / Chrome 88.0.4324.150

6 answers

  1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
     
    Hello,
    in your place, I would rather try using ConflictResolution.
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Furthermore, I am surprised by your use of
      chdir
      , the usual technique is to specify the full name:
      ActiveWorkbook.SaveAs Filename:="E:\A_Perso_10.09.19\Banque\Compta\Compta_Raymond.xlsm"
      0
  2. Anonymous user
     
    Hello,
    To complement the remark from yg_be that should resolve the issue (indicate the full path instead of ChDir, it should be noted that this command works like in DOS: The ChDir statement changes the default directory or folder but does not change the default drive. Another statement, ChDrive, changes the default drive.

    See: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/chdir-statement

    So to make it brief, you would need to do chdrive "E" in addition to chdir...

    One of the 2 solutions from yg_be is therefore more "elegant" to solve the problem.
    0
  3. Ramon1 Posted messages 364 Registration date   Status Member Last intervention  
     
    Thank you both for your answers,
    But I'm sorry, but you are not helping me much; I remind you that the code is not mine but generated by the recorder.
    So I'm not sure I understand your explanations, and you are not telling me why the recording works well for drive C:\ and not for drive E:\
    If I understand correctly, I should group the path into a single line as indicated by yg_be!
    I don't understand the use of ConflictResolution.
    Please, could you be more explicit and clearer for a beginner?

    A thousand apologies, I did a test before sending, and it seems that it works...
    Here is the modified code:

    Sub Enregist_C_et_E() ' ' Enregist_C_et_E Macro Sheets("Compta").Select Range("A3").Select Sheets("Echéance").Select ActiveSheet.Unprotect Password:="1447" Range("A1").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 13434879 .TintAndShade = 0 .PatternTintAndShade = 0 End With ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="1447" Sheets("Compta").Select Range("A4").Select Range("A4").End(xlDown).Offset(1, 0).Select ActiveWorkbook.Saved = True ChDir "C:\temp" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="Compta_Raymond.xlsm" ' Ecrasement fichier existant.xls' Application.DisplayAlerts = True 'Remettre absolument ensuite 'ChDir "E:\A_Perso_10.09.19\Banque\Compta" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="E:\A_Perso_10.09.19\Banque\Compta\Compta_Raymond.xlsm" 'ActiveWorkbook.SaveAs Filename:="Compta_Raymond.xlsm" ' Ecrasement fichier existant.xls' Application.DisplayAlerts = True 'Remettre absolument ensuite Application.Quit End Sub
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      An example of using conflict resolution:
      ActiveWorkbook.SaveAs _ Filename:="C:\temp\Compta_Raymond.xlsm", _ ConflictResolution:=xlLocalSessionChanges

      I find this preferable to manipulating Application.DisplayAlerts.
      0
  4. Anonymous user
     
    Hello,
    So it works now because the registration on "E" explicitly indicates the complete path to the file. There is no alert since the line "Application.DisplayAlerts = False" disables the dialog box during the save (we set it back to true afterwards).

    Why didn't it work before? Because by default, we are on drive "C". When we do ChDir "E:\A_Perso_10.09.19\Banque\Compta" we don’t change drives AND in reality, we remain on "C". So when we then do ActiveWorkbook.SaveAs Filename:="Compta_Raymond.xlsm", it bugs!

    To better understand, perform a test in a command prompt and type what is in bold below before pressing Enter:
    1. chdir -> ChDir without parameters displays the name of the current directory (dir additionally displays the list of files)
    2. ChDir "E:\A_Perso_10.09.19\Banque\Compta" -> Nothing seems to happen
    3. chdir -> displays the same thing as in 1
    4. E: -> takes you to the directory you typed in 2 (i.e., ChDrive "E" in VBA)

    So in your program, it’s as if you were in building C and you simply looked toward building E across the street. To drop a letter in building E, you need to go down and cross the street! Hence the ChDrive command I mentioned.
    You should have included a ChDrive in your program as well:
    ChDrive "E" ChDir "E:\A_Perso_10.09.19\Banque\Compta" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="Compta_Raymond.xlsm" ' Overwriting existing file.xls' Application.DisplayAlerts = True 'Must reset afterwards


    The solution by yg_be avoids switching drives constantly and sends the file to the right place without having to cross the street every time: a kind of teleportation!

    PS ConflictResolution. would allow replacing the displayAlerts that we set to false then true but without changing your saveAs, it wouldn't have changed the underlying problem for saving in E.
    0
  5. Ramon1 Posted messages 364 Registration date   Status Member Last intervention  
     
    Hello Roma,
    Thank you for your clear and concise explanations, your examples resonate with me... now I understand my mistake.

    To finish, I can therefore also replace the code:

    <code basic>ChDir "C:\temp" Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="Compta_Raymond.xlsm" ' Overwrite existing file.xls' Application.DisplayAlerts = True 'Must absolutely reset afterwards with Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="C:\temp\Compta_Raymond.xlsm" Application.DisplayAlerts = True 

    </code>
    0
    1. Anonymous user
       
      YES. It will be better this way even if it's working well for now: it will prevent a ChDrive added later from causing a bug in this procedure!
      0
    2. Ramon1 Posted messages 364 Registration date   Status Member Last intervention   > Anonymous user
       
      So if I understand correctly, the conflictresolution code also allows not to display the confirmation window.

      For the backup to C:\ and E:\ it's just a test; afterwards, I will of course back up to my external drive.

      In fact, the recorder's practice is not always a reliable source, but for beginners like me, it allows you to quickly and easily create a lot of macros.
      0
    3. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Ramon1 Posted messages 364 Registration date   Status Member Last intervention  
       
      the conflict resolution code prevents display and allows you to decide what to do in case of a conflict (file present).
      Application.DisplayAlerts is more general and often risks hiding real problems.
      What you had here, since you were not notified that the file was not saved.
      0
    4. Ramon1 Posted messages 364 Registration date   Status Member Last intervention   > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
       
      Thank you to you and Roma for your help, I have marked the question as resolved.
      0
    5. Ramon1 Posted messages 364 Registration date   Status Member Last intervention   > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
       
      I'm sorry, I'm getting back to you. I replaced the code as you advised with ConflictResolution,
      the saving is done correctly on both drives, but the confirmation dialog box opens 2 times...
      What did I forget?

      Sub Enregist_C_et_E() ' ' Enregist_C_et_E Macro Sheets("Compta").Select Range("A3").Select Sheets("Echéance").Select ActiveSheet.Unprotect Password:="1447" Range("A1").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 13434879 .TintAndShade = 0 .PatternTintAndShade = 0 End With ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="1447" Sheets("Compta").Select Range("A4").Select Range("A4").End(xlDown).Offset(1, 0).Select ActiveWorkbook.Saved = True ActiveWorkbook.SaveAs _ Filename:="C:\temp\Compta_Raymond.xlsm", _ ConflictResolution:=xlLocalSessionChanges 'Application.DisplayAlerts = False 'ActiveWorkbook.SaveAs Filename:="C:\temp\Compta_Raymond.xlsm" ' Ecrasement fichier existant.xls' 'Application.DisplayAlerts = True 'Remettre absolument ensuite ActiveWorkbook.SaveAs _ Filename:="E:\A_Perso_10.09.19\Banque\Compta\Compta_Raymond.xlsm", _ ConflictResolution:=xlLocalSessionChanges 'Application.DisplayAlerts = False 'ActiveWorkbook.SaveAs Filename:="E:\A_Perso_10.09.19\Banque\Compta\Compta_Raymond.xlsm" ' Ecrasement fichier existant.xls' 'Application.DisplayAlerts = True 'Remettre absolument ensuite Application.Quit End Sub
      0
  6. Anonymous user
     
    I'm wondering what the practical interest is in making a backup in the C:\temp folder AND in the folder on "E"? Are these duplicates intentional?
    0