MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2002, 12:00 PM   #1
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Good day all
Trying to remind the user to save a template as something else. This must be basic VBA but I can't put my finger on the problem.
I get run time error 91 object variable not set with this.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim MyWkbk As Workbook

If MyWkbk.Name = Book3.xls Then

MsgBox prompt:="Don't step on the template"

End If
End Sub
GeorgeB is offline   Reply With Quote
Old Mar 24th, 2002, 12:03 PM   #2
Anne Troy
MrExcel MVP
 
Anne Troy's Avatar
 
Join Date: Feb 2002
Location: Allentown, PA
Posts: 2,510
Default

Why not just make it read-only so that they cannot?
__________________
~Anne Troy
Anne Troy is offline   Reply With Quote
Old Mar 24th, 2002, 01:27 PM   #3
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Copy this code into your workbook class module.
Make sure the dim statement is put in the general declarations part of the module(The very top)

This will force the user to save the file with a different name by having the save as dialog pop up and prompt for a filename.
It will not allow the active workbook to be saved with the same name anywhere on your computer.


Option Explicit
Public SavedAlready As Boolean


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fName
If SavedAlready = True Then GoTo SaveNot
ReSave:
Do Until fName <> False And fName <> ActiveWorkbook.Path & "" & ActiveWorkbook.Name
fName = Application.GetSaveAsFilename("SaveNewName", Filefilter:="Excel Worksheet (*.xls), *.xls")
Loop
If LCase(Right(fName, Len(ActiveWorkbook.Name))) = LCase(ActiveWorkbook.Name) Then
MsgBox "Cannot save with that name. Please choose another"
fName = False
GoTo ReSave
End If
SavedAlready = True
SaveNot:
ActiveWorkbook.SaveAs fName
End Sub
Tom Schreiner is offline   Reply With Quote
Old Mar 24th, 2002, 01:56 PM   #4
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Thanks for your replies gentlemen. Perhaps I should have explained it better. I have a large template to do construction bidding on. It resides on the company server so saving it as a template on each computer is not an option for maintenance reasons. Once in a while an estimator starts a job and saves it without renaming it thereby "stepping on the template" and I have to remove the data to return it to a usable state. I want to check it before save for a specific name and remind them not to "step on the template" but allow standard save after it is renamed.
Thanks for your help.
George
GeorgeB is offline   Reply With Quote
Old Mar 24th, 2002, 03:37 PM   #5
Anne Troy
MrExcel MVP
 
Anne Troy's Avatar
 
Join Date: Feb 2002
Location: Allentown, PA
Posts: 2,510
Default

Hello, George.

Try this. It *almost* forces your guys to give the file a name right away. This can keep them from forgetting.

Private Sub Workbook_Open()

Dim Msg, Style, Title, Response, MyString
Msg = "Please save the file to a new name." ' Define message.
Style = vbOKCancel + vbExclamation ' Define buttons.
Title = "Save File" ' Define title.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbOK Then ' User chose Ok.
Application.Dialogs(xlDialogSaveAs).Show ("*.xls")

Else: Application.ActiveWorkbook.Close False

End If
End Sub

Hope it helps.
__________________
~Anne Troy
Anne Troy is offline   Reply With Quote
Old Mar 24th, 2002, 03:49 PM   #6
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Youre the man Dreamboat.
Nice piece of code that. Just what the doc
ordered. Thanks
Have a great day
George
GeorgeB is offline   Reply With Quote
Old Mar 24th, 2002, 05:27 PM   #7
Anne Troy
MrExcel MVP
 
Anne Troy's Avatar
 
Join Date: Feb 2002
Location: Allentown, PA
Posts: 2,510
Default

Youre the man Dreamboat.

My husband might be surprised to find that out!!



Happy to help. Have to tell you, that's the first piece of code I did virtually on my own. (That means without stealing from somebody else first.)
__________________
~Anne Troy
Anne Troy is offline   Reply With Quote
Old Mar 24th, 2002, 06:29 PM   #8
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Sorry Dreamboat
You're the WOman then. One more ? if you care to respond. Any way to test for a specific name (the original name) and not run the code if the workbook has already been saved with a new name and put into use??
GeorgeB is offline   Reply With Quote
Old Mar 24th, 2002, 07:34 PM   #9
GeorgeB
Board Regular
 
Join Date: Feb 2002
Location: Las Vegas Nevada USA
Posts: 240
Default

Re: My last post. This is the code I'm trying but get a "type mismatch" error on the .Name part of the Set line.

Private Sub Workbook_Open()
'Display plan log message
UserForm1.Show
On Error Resume Next
Dim wBook As Workbook
Set wBook = (Application.ActiveWorkbook.Name)
If wBook.Name <> ("Nevada Estimating 5.0.xls") Then
End If
Exit Sub
Dim Msg, Style, Title, Response, MyString
Msg = "PLEASE SAVE THE FILE TO A NEW NAME." ' Define message.
Style = vbOKCancel + vbExclamation ' Define buttons.
Title = " DONT STEP ON THE TEMPLATE" ' Define title.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbOK Then ' User chose Ok.
Application.Dialogs(xlDialogSaveAs).Show ("*.xls")

Else: Application.ActiveWorkbook.Close False

End If
End Sub
GeorgeB is offline   Reply With Quote
Old Mar 24th, 2002, 08:05 PM   #10
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi George


I am a bit lost in what you are doing but try this:

Private Sub Workbook_Open()
Dim wBook As Workbook
Dim Response As Integer
'Display plan log message
UserForm1.Show


Set wBook = ActiveWorkbook
If wBook.Name <> "Nevada Estimating 5.0.xls" Then
End If

Exit Sub 'SHOULD THIS BE BEFORE THE ABOVE "END IF"?


Response = MsgBox("PLEASE SAVE THE FILE TO A NEW NAME.", _
vbOKCancel + vbExclamation, " DONT STEP ON THE TEMPLATE")

If Response = vbOK Then ' User chose Ok.
Application.Dialogs(xlDialogSaveAs).Show ("*.xls")

Else: Application.ActiveWorkbook.Close False

End If
End Sub


I think you really want the "Exit Sub" moved before the "End If" ?
Dave Hawley is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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 On

Forum Jump


All times are GMT -4. The time now is 08:31 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes