View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Patrick Riley Patrick Riley is offline
external usenet poster
 
Posts: 34
Default CATCH-22 won't let me save the file

Dave:
I opened VBE, hit Ctrl-G, the Immediate Window opened, I cut and pasted the
"application.enableevents = false" code (without the quote marks) into
Immediate Window. When I tried to save the file, however, the beforesave
code fired, producing the warning message to type in the name. Incidentally,
the code I am using is:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If IsEmpty(Sheets("Main").Range("E59").Value) Then
MsgBox "Cell E59 of sheet 'Main' must be filled in before " & _
"this file can be saved.", 16, "ERROR"
Cancel = True
End If
End Sub

Also, re your suggestion to check the username, under ToolsOptionsGeneral,
my username is pjriley, so I presumed I should use "pjriley" instead of
"Patrick Riley" in the code that you provided. I tried it, and IT LET ME
SAVE THE FILE with the BLANK name field!! BUT...when I e-mailed the file to
another user and tested it at his machine under his login and application
name (making sure the VB code was still there and unchanged), it did NOT
prompt me to fill in the name field. Under ToolsOptionsGeneral, his
username was "acutti", as it should have been.
What do you make of this?
---Pat

"Dave Peterson" wrote:

Before you (as the developer, not the user) save the file, you can turn off
events.

Open the VBE
hit ctrl-g to see the immediate window
type this and hit enter:
application.enableevents = false

Then save your workbook--the _beforesave event won't fire.

Then back to the VBE and toggle the setting in the immediate window:
application.enableevents = true

====
You could also do other stuff, too. Maybe check the username?

if lcase(application.username) = lcase("Patrick Riley") then
'do nothing, let it save...
'or clear that cell!
me.sheets("Main").range("e59").value = ""
else
If IsEmpty(me.Sheets("Main").Range("E59").Value) Then
MsgBox "You must type in your name before " & _
"this file can be saved.", 16, "ERROR"
Cancel = True
End If
end if



Patrick Riley wrote:

Thanks to some generous help from users of this site, I, with no Visual Basic
knowledge, am using some VB code that requires the user to type his/her name
into a given cell before the file can be saved, since the name must appear on
the printed version. The user-name field is intentionally left blank. Upon
attempting to save, the code presents a message to type in the name, if the
name-field is still blank.
Well, CATCH-22. This very code is preventing me from leaving the name field
blank when I try to save the file in final form, ready for enduser use.
The code I am using is:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
If IsEmpty(Sheets("Main").Range("E59").Value) Then
MsgBox "You must type in your name before " & _
"this file can be saved.", 16, "ERROR"
Cancel = True
End If
End Sub

Any suggestions?
---Pat Riley


--

Dave Peterson