Beforesave - Save copy on my desktop

J

J.W. Aldridge

Hi.

I have this code that i was running "beforeclose", however I adjusted
it to try to make it a "beforesave" code however, it doesnt work. No
error messages, but when i check my desktop, its not there.


Any advice?


Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean, ByRef Cancel As
Boolean)
Application.DisplayAlerts = False
Set WshShell = CreateObject("WScript.Shell")
ThisWorkbook.SaveAs Filename:=WshShell.SpecialFolders("Desktop") &
"\" & ThisWorkbook.Name
Application.DisplayAlerts = True
End Sub
 
C

Chip Pearson

Are you sure you want SaveAs rather than SaveCopyAs? Also, you should
disable events before doing the save -- otherwise you'll loop.

Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean, _
ByRef Cancel As Boolean)
Dim FName As String
On Error GoTo EXIT_SUB:
Application.EnableEvents = False
Application.DisplayAlerts = False
Set WshShell = CreateObject("WScript.Shell")
FName = WshShell.SpecialFolders("Desktop") & _
"\" & ThisWorkbook.Name
ThisWorkbook.SaveCopyAs Filename:=FName
EXIT_SUB:
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
J

J.W. Aldridge

WOW....The Legend Himself???
(Man, I am honored!)


Yes, I believe Save a copy as is what I am trying to do.
Would that change the code recommended?
 
C

Chip Pearson

It shouldn't make a difference in the code whether you use SaveAs or
SaveCopyAs.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top