PC Review


Reply
Thread Tools Rate Thread

Closing without saving and without asking the user to save...or no

 
 
=?Utf-8?B?Y3VzaA==?=
Guest
Posts: n/a
 
      7th Aug 2007
I want to close a file without saving it and without asking the user whether
to save or not.

In the BeforeClose code below this works when the Backup_Transactions code
is remove or bypassed. When I include that procedure, the user is asked if
he wants to save the file -- even when I have included ThisWorkbook.Saved
= True

If I enter code like Applications.DisplayAlerts=False, there is no difference.

Application.Quit caused a fatal error-- apparently quit before Excel could
properly shut itself down

Any suggestions?

'''''''''''''''''''''''''''
In ThisWorkBook mod:
''''''''''''''''''''''''''''

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.ScreenUpdating = False
On Error Resume Next
DevMode 'Restores toolbars
Backup_Transactions

''DO NOT SAVE THIS WBK
''AND CLOSE WITHOUT ASKING THE USER
ThisWorkbook.Saved = True
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
In a standard module:
''''''''''''''''''''''''''''''''''''''''''''''
Sub Backup_Transactions()
Dim FilePath As String
Dim FileName As String
Dim FileExtStr As String
Dim wb As Workbook
Dim iMsg As Object
Dim iConf As Object

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

''COPY TRANSACTION DB
Sheets("TRANSACTIONS").Range("xDB").Copy

''PASTE TO A NEW SHEET
Sheets.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Name = "Sheet1"
''MOVE NEW SHEET TO A NEW WORKBOOK
ActiveSheet.Move

''SAVE NEW WBK THEN CLOSE IT
Set wb = ThisWorkbook
FilePath = wb.Path & "\Backup\Transactions\"
FileName = "TRANSACTIONS" & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
".", , 1)))
ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr

''SAVE A COPY TO DATA FOLDER
FilePath = wb.Path & "\Data\Transactions\"
FileName = "TRANSACTIONS"
FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
".", , 1)))
''Disable alert so it will overwrite last copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
ActiveWorkbook.Close
Application.DisplayAlerts = True


Tidy:
With Application
.ScreenUpdating = True
.EnableEvents = True

End With

Set wb = Nothing
Set iMsg = Nothing
Set iConf = Nothing
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      7th Aug 2007
Add this line...

ThisWorkbook.Close SaveChanges:=False
--
HTH...

Jim Thomlinson


"cush" wrote:

> I want to close a file without saving it and without asking the user whether
> to save or not.
>
> In the BeforeClose code below this works when the Backup_Transactions code
> is remove or bypassed. When I include that procedure, the user is asked if
> he wants to save the file -- even when I have included ThisWorkbook.Saved
> = True
>
> If I enter code like Applications.DisplayAlerts=False, there is no difference.
>
> Application.Quit caused a fatal error-- apparently quit before Excel could
> properly shut itself down
>
> Any suggestions?
>
> '''''''''''''''''''''''''''
> In ThisWorkBook mod:
> ''''''''''''''''''''''''''''
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
>
> Application.ScreenUpdating = False
> On Error Resume Next
> DevMode 'Restores toolbars
> Backup_Transactions
>
> ''DO NOT SAVE THIS WBK
> ''AND CLOSE WITHOUT ASKING THE USER
> ThisWorkbook.Saved = True
> End Sub
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''
> In a standard module:
> ''''''''''''''''''''''''''''''''''''''''''''''
> Sub Backup_Transactions()
> Dim FilePath As String
> Dim FileName As String
> Dim FileExtStr As String
> Dim wb As Workbook
> Dim iMsg As Object
> Dim iConf As Object
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> ''COPY TRANSACTION DB
> Sheets("TRANSACTIONS").Range("xDB").Copy
>
> ''PASTE TO A NEW SHEET
> Sheets.Add
> ActiveSheet.Paste
> Application.CutCopyMode = False
> ActiveSheet.Name = "Sheet1"
> ''MOVE NEW SHEET TO A NEW WORKBOOK
> ActiveSheet.Move
>
> ''SAVE NEW WBK THEN CLOSE IT
> Set wb = ThisWorkbook
> FilePath = wb.Path & "\Backup\Transactions\"
> FileName = "TRANSACTIONS" & Format(Now, "dd-mmm-yy h-mm-ss")
> FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> ".", , 1)))
> ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
>
> ''SAVE A COPY TO DATA FOLDER
> FilePath = wb.Path & "\Data\Transactions\"
> FileName = "TRANSACTIONS"
> FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> ".", , 1)))
> ''Disable alert so it will overwrite last copy
> Application.DisplayAlerts = False
> ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
> ActiveWorkbook.Close
> Application.DisplayAlerts = True
>
>
> Tidy:
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
>
> End With
>
> Set wb = Nothing
> Set iMsg = Nothing
> Set iConf = Nothing
> End Sub
>

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      7th Aug 2007
Sorry I should have looked more closely at your code. In your before close
you call Backup_Transactions which has the line ActiveWorkbook.Close in it.
With events disabled it will close the book without executing your
ThisWorkbook.Saved = True...

Chnage the ActiveWorkbook.Close to
ThisWorkbook.Close SaveChanges:=False
(or ActiveWorkbook.Close SaveChanges:=False)
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> Add this line...
>
> ThisWorkbook.Close SaveChanges:=False
> --
> HTH...
>
> Jim Thomlinson
>
>
> "cush" wrote:
>
> > I want to close a file without saving it and without asking the user whether
> > to save or not.
> >
> > In the BeforeClose code below this works when the Backup_Transactions code
> > is remove or bypassed. When I include that procedure, the user is asked if
> > he wants to save the file -- even when I have included ThisWorkbook.Saved
> > = True
> >
> > If I enter code like Applications.DisplayAlerts=False, there is no difference.
> >
> > Application.Quit caused a fatal error-- apparently quit before Excel could
> > properly shut itself down
> >
> > Any suggestions?
> >
> > '''''''''''''''''''''''''''
> > In ThisWorkBook mod:
> > ''''''''''''''''''''''''''''
> >
> > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> >
> > Application.ScreenUpdating = False
> > On Error Resume Next
> > DevMode 'Restores toolbars
> > Backup_Transactions
> >
> > ''DO NOT SAVE THIS WBK
> > ''AND CLOSE WITHOUT ASKING THE USER
> > ThisWorkbook.Saved = True
> > End Sub
> > '''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > In a standard module:
> > ''''''''''''''''''''''''''''''''''''''''''''''
> > Sub Backup_Transactions()
> > Dim FilePath As String
> > Dim FileName As String
> > Dim FileExtStr As String
> > Dim wb As Workbook
> > Dim iMsg As Object
> > Dim iConf As Object
> >
> > With Application
> > .ScreenUpdating = False
> > .EnableEvents = False
> > End With
> >
> > ''COPY TRANSACTION DB
> > Sheets("TRANSACTIONS").Range("xDB").Copy
> >
> > ''PASTE TO A NEW SHEET
> > Sheets.Add
> > ActiveSheet.Paste
> > Application.CutCopyMode = False
> > ActiveSheet.Name = "Sheet1"
> > ''MOVE NEW SHEET TO A NEW WORKBOOK
> > ActiveSheet.Move
> >
> > ''SAVE NEW WBK THEN CLOSE IT
> > Set wb = ThisWorkbook
> > FilePath = wb.Path & "\Backup\Transactions\"
> > FileName = "TRANSACTIONS" & Format(Now, "dd-mmm-yy h-mm-ss")
> > FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> > ".", , 1)))
> > ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
> >
> > ''SAVE A COPY TO DATA FOLDER
> > FilePath = wb.Path & "\Data\Transactions\"
> > FileName = "TRANSACTIONS"
> > FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> > ".", , 1)))
> > ''Disable alert so it will overwrite last copy
> > Application.DisplayAlerts = False
> > ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
> > ActiveWorkbook.Close
> > Application.DisplayAlerts = True
> >
> >
> > Tidy:
> > With Application
> > .ScreenUpdating = True
> > .EnableEvents = True
> >
> > End With
> >
> > Set wb = Nothing
> > Set iMsg = Nothing
> > Set iConf = Nothing
> > End Sub
> >

 
Reply With Quote
 
=?Utf-8?B?Y3VzaA==?=
Guest
Posts: n/a
 
      8th Aug 2007
Jim,

thanks for the reply, but we need a closer look still ..........

The ActiveWorkbook in the Backup_Transactions sub is just a
copy of the Transactions worksheet from the original ThisWorkbook.

This ActiveWorkbook was created with the statement
ActiveSheet.Move
I then save-as a couple of times, then close the ActiveWbk
with ActiveWorkbook.Close

At that point I am back to my original wbk whick I want to
now close without giving the user the option of saving.
Hence: ThisWorkbook.Saved = True
Unfortunately, I am still then presented with the Want-to-Save? dialog


"Jim Thomlinson" wrote:

> Sorry I should have looked more closely at your code. In your before close
> you call Backup_Transactions which has the line ActiveWorkbook.Close in it.
> With events disabled it will close the book without executing your
> ThisWorkbook.Saved = True...
>
> Chnage the ActiveWorkbook.Close to
> ThisWorkbook.Close SaveChanges:=False
> (or ActiveWorkbook.Close SaveChanges:=False)
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jim Thomlinson" wrote:
>
> > Add this line...
> >
> > ThisWorkbook.Close SaveChanges:=False
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "cush" wrote:
> >
> > > I want to close a file without saving it and without asking the user whether
> > > to save or not.
> > >
> > > In the BeforeClose code below this works when the Backup_Transactions code
> > > is remove or bypassed. When I include that procedure, the user is asked if
> > > he wants to save the file -- even when I have included ThisWorkbook.Saved
> > > = True
> > >
> > > If I enter code like Applications.DisplayAlerts=False, there is no difference.
> > >
> > > Application.Quit caused a fatal error-- apparently quit before Excel could
> > > properly shut itself down
> > >
> > > Any suggestions?
> > >
> > > '''''''''''''''''''''''''''
> > > In ThisWorkBook mod:
> > > ''''''''''''''''''''''''''''
> > >
> > > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> > >
> > > Application.ScreenUpdating = False
> > > On Error Resume Next
> > > DevMode 'Restores toolbars
> > > Backup_Transactions
> > >
> > > ''DO NOT SAVE THIS WBK
> > > ''AND CLOSE WITHOUT ASKING THE USER
> > > ThisWorkbook.Saved = True
> > > End Sub
> > > '''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > > In a standard module:
> > > ''''''''''''''''''''''''''''''''''''''''''''''
> > > Sub Backup_Transactions()
> > > Dim FilePath As String
> > > Dim FileName As String
> > > Dim FileExtStr As String
> > > Dim wb As Workbook
> > > Dim iMsg As Object
> > > Dim iConf As Object
> > >
> > > With Application
> > > .ScreenUpdating = False
> > > .EnableEvents = False
> > > End With
> > >
> > > ''COPY TRANSACTION DB
> > > Sheets("TRANSACTIONS").Range("xDB").Copy
> > >
> > > ''PASTE TO A NEW SHEET
> > > Sheets.Add
> > > ActiveSheet.Paste
> > > Application.CutCopyMode = False
> > > ActiveSheet.Name = "Sheet1"
> > > ''MOVE NEW SHEET TO A NEW WORKBOOK
> > > ActiveSheet.Move
> > >
> > > ''SAVE NEW WBK THEN CLOSE IT
> > > Set wb = ThisWorkbook
> > > FilePath = wb.Path & "\Backup\Transactions\"
> > > FileName = "TRANSACTIONS" & Format(Now, "dd-mmm-yy h-mm-ss")
> > > FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> > > ".", , 1)))
> > > ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
> > >
> > > ''SAVE A COPY TO DATA FOLDER
> > > FilePath = wb.Path & "\Data\Transactions\"
> > > FileName = "TRANSACTIONS"
> > > FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name,
> > > ".", , 1)))
> > > ''Disable alert so it will overwrite last copy
> > > Application.DisplayAlerts = False
> > > ActiveWorkbook.SaveAs FilePath & FileName & FileExtStr
> > > ActiveWorkbook.Close
> > > Application.DisplayAlerts = True
> > >
> > >
> > > Tidy:
> > > With Application
> > > .ScreenUpdating = True
> > > .EnableEvents = True
> > >
> > > End With
> > >
> > > Set wb = Nothing
> > > Set iMsg = Nothing
> > > Set iConf = Nothing
> > > End Sub
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompting user to answer a question before saving and closing wkbo =?Utf-8?B?SnVzdEJyZWF0aGU=?= Microsoft Excel Programming 5 8th Mar 2007 10:55 PM
Closing the Open Save Dialog when user selects Save BusyBoy Microsoft ASP .NET 2 31st Oct 2006 03:46 PM
VBA - closing without saving changes ajliaks Microsoft Excel Programming 2 21st Apr 2004 09:28 PM
delay saving user settings (slow profile save) cosmic Microsoft Windows 2000 0 11th Sep 2003 07:01 PM
prevent user from saving file to a folder but allow my code to save from behind. susie Microsoft Excel Programming 3 25th Jul 2003 03:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:50 AM.