Excel VBA, If User Doesn't Save File Elsewhere, Then Exit Workbook

Hello,

I have the following code:

If ActiveWorkbook.Path <> "S:\.Cerberus Publication" Then
'Do Nothing
Else
MsgBox ("Save File on YOUR PC; You Cannot Use S:\ Drive Copy.")
Application.Dialogs(xlDialogSaveAs).Show ("*.xlsb")

Open in new window


What do I need to add to it in orderto make it exit the workbook if someone exits the file save as dialog that appears? Also, where should I put this code so it fires up when the workbook is opened.
EnigmaMatterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Place in the ThisWorkbook module

Private Sub Workbook_Open()
If ActiveWorkbook.Path <> "S:\.Cerberus Publication" Then
'Do Nothing
Else
MsgBox ("Save File on YOUR PC; You Cannot Use S:\ Drive Copy.")
res = Application.GetSaveAsFilename(InitialFileName:=".xlsb", _
        FileFilter:="Excel Binary files (*.xlsb), *.xlsb, All files (*.*), *.*")

If res = 0 Or res Like "S:\*" Then
    Thisworbook.Close False
Else
    ThisWorkbook.SaveAs res
End If
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EnigmaMatterAuthor Commented:
Hello,

If I click cancel, it errors on Line 10.

Also, it seems to be missing an End If?
0
EnigmaMatterAuthor Commented:
I added in the End If and corrected the typo. It works! Thanks!
0
Rgonzo1971Commented:
Sorry

replace line 10 with
ThisWorkBook.Close False
0
EnigmaMatterAuthor Commented:
There needs to be another End If at the end (before End Sub), thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.