File Saving Script Problems

mike79

New Member
Joined
Oct 31, 2005
Messages
14
I have this script that auto saves a document:

Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
Sheets("Sheet1").Range("a1") = " " & Date & " klokken " & Time
ActiveWorkbook.SaveAs "C:\Temp\Dreports" & Format(Date, "mmddyy") & ".xls"
End Sub


It does what I want, but crash's everytime it saves.
Is it something I put in that is wrong?

My file is a 4 sheet document.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
L

Legacy 98055

Guest
I think you have entered loopyville. :LOL: Every time your code calls the SaveAs method, you are causing the Workbook_BeforeSave event to fire again and again and again and again... Disable events before you SaveAs and then re-enable events afterwards...

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_BeforeSave(ByVal SaveAsUi <font color="#0000A0">As</font> Boolean, Cancel <font color="#0000A0">As</font> Boolean)
       <font color="#0000A0">On</font> <font color="#0000A0">Error</font> <font color="#0000A0">GoTo</font> Err_Workbook_BeforeSave
       Application.EnableEvents = <font color="#0000A0">False</font>
       Sheets("Sheet1").Range("a1") = " " & <font color="#0000A0">Date</font> & " klokken " & Time
       ActiveWorkbook.SaveAs "C:\Temp\Dreports" & Format(Date, "mmddyy") & ".xls"
  Err_Workbook_BeforeSave:
       Application.EnableEvents = <font color="#0000A0">True</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("92520061621489").value=document.all("92520061621489").value.replace(/<br \/>\s\s/g,"");document.all("92520061621489").value=document.all("92520061621489").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("92520061621489").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="92520061621489" wrap="virtual">
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
On Error GoTo Err_Workbook_BeforeSave
Application.EnableEvents = False
Sheets("Sheet1").Range("a1") = " " & Date & " klokken " & Time
ActiveWorkbook.SaveAs "C:\Temp\Dreports" & Format(Date, "mmddyy") & ".xls"
Err_Workbook_BeforeSave:
Application.EnableEvents = True
End Sub</textarea>
 

Forum statistics

Threads
1,136,345
Messages
5,675,225
Members
419,555
Latest member
Paddington

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
Top