If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Code runs but doesn't save the file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-07, 08:46
Burnedout Burnedout is offline
Registered User
 
Join Date: Aug 2004
Location: Aurora, Ontario
Posts: 10
Question Code runs but doesn't save the file


I have a file which opens in one directory and when closed the user is presented with a different directory to save in.

The code attached lets me present the user with the proper name, allows you to click all of the right buttons but doesn't save the file.

If I click on an existing name it will let ask me to replace the file.

Any thoughts would be greatly appreicated.

***********Form Code Starts Here***********
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sQ As String
ThisWorkbook.Saved = True
sQ = MsgBox("Do you want to save this workbook ?", vbYesNoCancel)
If sQ = vbCancel Then Cancel = True
If sQ = vbYes Then
If FSaveAs = False Then
Cancel = True
End If
End If

End Sub


*******Module Code Starts Here***********

Option Explicit
Function FSaveAs() As Boolean

Dim sFileName As String
Dim InitialDir As String
Dim SaveAsDialog As FileDialog
Dim FileDest As Variant

ChDrive "F"
ChDir "F:\Public\Training Reports"

sFileName = Sheet1.[b5]


Set SaveAsDialog = Application.FileDialog(msoFileDialogSaveAs)
FileDest = "F:\Public\Training Reports"

With SaveAsDialog
.InitialFileName = sFileName

.Show

End With

On Error GoTo FileError

FSaveAs = True
Exit Function

FileError:
MsgBox "You forgot to select a Subject Name for this report.", vbRetryCancel

FSaveAs = False
End Function

********* All Code Ends Here*****************
Reply With Quote
  #2 (permalink)  
Old 07-27-07, 08:57
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
Post

I don't do a whole lot of event programming in Excel, but...

I think you just need to move your .Saved

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sQ As String
sQ = MsgBox("Do you want to save this workbook ?", vbYesNoCancel)
If sQ = vbCancel Then Cancel = True
If sQ = vbYes Then
  If FSaveAs = False Then
    Cancel = True
  else
     ThisWorkbook.Saved = True
  end if
End If
End Sub
__________________
Inspiration Through Fermentation
Reply With Quote
  #3 (permalink)  
Old 07-27-07, 11:08
norie norie is offline
Registered User
 
Join Date: Mar 2006
Posts: 163
Could the reason that nothing is saved be because there isn't actually any code that saves anything?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On