VALIDATE WB NAME

A

AD108

I attemped to use the following code to force users to save the workbook
with a certain name.
It is not working, and produces ambiguous results. Any ideas.

Thanks in advance.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim filename As String
filename = Application.GetSaveAsFilename
If Not UCase(filename) Like "*AIR CONTAINER*" Then
MsgBox "Save with correct name"
Cancel = True
Else
Cancel = False
End If

End Sub
 
B

Bob Phillips

Like should work okay.

To the OP, what do you mean by ambiguous results? I note you are not testing
whether they have used the SaveAs or Save.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
A

AD108

Hi Bob,

How would I test for either save or saveas.

With the code I wrote, it made the msgbox come up as intented, but when I
close the message box, the getfilename dialouge pops up again. It then
allows the user to save it as whatever name they put in.

Maybe it is not going to work in the "before save" event handler?

Bob Phillips said:
Like should work okay.

To the OP, what do you mean by ambiguous results? I note you are not testing
whether they have used the SaveAs or Save.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bob Phillips

The BeforeSave event has the SaveAsUI argument which is rue for SaveAs,
False for Save.

I don't think you necessarily need it, as if this is a new workbook, SaveAs
is the default, so you could just use

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim sFilename As String
On Error GoTo wb_exit
Application.EnableEvents = False
Cancel = True
sFilename = Application.GetSaveAsFilename
If Not UCase(sFilename) Like "*AIR CONTAINER*" Then
MsgBox "Save with correct name"
Else
ThisWorkbook.SaveAs filename:=sFilename
End If
wb_exit:
Application.EnableEvents = True
End Sub

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
A

AD108

Thank you very much,

AD108

Bob Phillips said:
The BeforeSave event has the SaveAsUI argument which is rue for SaveAs,
False for Save.

I don't think you necessarily need it, as if this is a new workbook, SaveAs
is the default, so you could just use

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim sFilename As String
On Error GoTo wb_exit
Application.EnableEvents = False
Cancel = True
sFilename = Application.GetSaveAsFilename
If Not UCase(sFilename) Like "*AIR CONTAINER*" Then
MsgBox "Save with correct name"
Else
ThisWorkbook.SaveAs filename:=sFilename
End If
wb_exit:
Application.EnableEvents = True
End Sub

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 

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