VBA - After Disabling 'Save" Workbook takes long time to save

AC1982

New Member
Joined
Jul 9, 2009
Messages
40
Hi,

I disabled the save function on a workbook in order to prevent users from the saving the file outside a specific directory.

My only problem is that after doing that I have noticed that file takes a lot longer to save, and that a save window pops up with a progress bar. In some cases this bar pops up after the file has actually saved.

This is my code, if anyone can see any errors please let me know -

Code:
[SIZE=3][FONT=Calibri]Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim OutApp As Object<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim OutMail As Object<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim hlink As Hyperlink '?<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim strPath As String<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     A = MsgBox("Do you really want to save the workbook?", vbYesNo)<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    If A = vbNo Then<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Cancel = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Else<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        '// Get the save path from the user<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       strPath = Application.GetSaveAsFilename(FileFilter:="Excel Files,*.xls,All Files,*.*", _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]            Title:="Save As File Name")<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        '// Check they didnt press cancel<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        If strPath = "False" Then<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        Cancel = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     Else<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        '// Check drive letter is G<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        If Left(strPath, 28) = "G:\Commercial Services Group"   Then<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                '// Disable events and try to save<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                Application.EnableEvents = False<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                ActiveWorkbook.SaveAs Filename:=strPath<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                Application.EnableEvents = True<o:p></o:p>[/FONT][/SIZE]
<o:p>[FONT=Calibri][SIZE=3] [/SIZE][/FONT]</o:p>
[SIZE=3][FONT=Calibri]            Else<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]            <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                   MsgBox "You are receiving this error message because you tried to save this file outside of the G:\Tendering directory, please try again and this time save your file in G:\Tendering", vbExclamation, "File Not Saved!"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Cancel = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]       End If<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      End If<o:p></o:p>[/FONT][/SIZE]
<o:p>[FONT=Calibri][SIZE=3] [/SIZE][/FONT]</o:p>
[SIZE=3][FONT=Calibri]      End If<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]              With Application<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]            .ScreenUpdating = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]            .EnableEvents = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]             On Error GoTo 0<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        End With<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    '---------<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]     Cancel = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    End Sub<o:p></o:p>[/FONT][/SIZE]
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,224,586
Messages
6,179,718
Members
452,939
Latest member
WCrawford

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