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 Apr 22nd, 2002, 06:55 AM   #1
Michael Pettinicchi
Board Regular
 
Join Date: Apr 2002
Posts: 53
Default

I say for "For EXPERTS Only" because to-date, nobody has been able to figure this one out and those who tried have given up and never replied to my replies to their replies.

I have the following code in the "Workbook" of "ThisWorkbook". It works if I open the file directly, i.e., it does the job and closes. But, if I open the file via another Excel file, it does the job but doesn't close. What's wrong?

Private Sub Workbook_Open()
Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
ThisWorkbook.Close
End Sub

Thanks
Michael
Michael Pettinicchi is offline   Reply With Quote
Old Apr 22nd, 2002, 07:05 AM   #2
memicol
 
Join Date: Apr 2002
Posts: 16
Default

Not tested, but try ActiveWorkbook.Close
memicol is offline   Reply With Quote
Old Apr 22nd, 2002, 08:09 AM   #3
Tim Francis-Wright
Board Regular
 
Join Date: Feb 2002
Posts: 74
Default

Curiouser and curiouser...

I could duplicate the problem not only when VBA opened the file from another workbook, but also whenever another workbook was open.

Stepping through the macro triggered the closing of the workbook. Simply running it did not. (This isn't the first time I've
seen Workbook_Open generate frustration.)

I do have a workaround for you:
Put the code that you want into a normal
module, under Auto_Open():

Private Sub Auto_Open()
Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
ThisWorkbook.Close
End Sub

__________________
"Interfere? Of course we should interfere! Always do what you're best at, that's what I say!" -- The Doctor, Nightmare of Eden
Tim Francis-Wright is offline   Reply With Quote
Old Apr 22nd, 2002, 08:18 AM   #4
memicol
 
Join Date: Apr 2002
Posts: 16
Default

But will Auto_Open work when the file is opened by a macro in another workbook ?
memicol is offline   Reply With Quote
Old Apr 22nd, 2002, 08:28 AM   #5
Tim Francis-Wright
Board Regular
 
Join Date: Feb 2002
Posts: 74
Default

Use the following syntax:
Workbooks.Open "ANALYSIS.XLS"
ActiveWorkbook.RunAutoMacros xlAutoOpen
__________________
"Interfere? Of course we should interfere! Always do what you're best at, that's what I say!" -- The Doctor, Nightmare of Eden
Tim Francis-Wright is offline   Reply With Quote
Old Apr 22nd, 2002, 08:30 AM   #6
klb
Board Regular
 
Join Date: Apr 2002
Location: Minnesota
Posts: 821
Default

Yes Auto_open will work even when the workbook is opened by a macro in another file. Anyway mine do.



[ This Message was edited by: klb on 2002-04-22 07:31 ]
klb is offline   Reply With Quote
Old Apr 22nd, 2002, 08:40 AM   #7
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

OK, I ran your code and the first time I opened the workbook, the macro warning did not show. I didn't change the code, but I did change your code into a subroutine in a module. I then called that subroutine from the Open event and it seemed to work.

Here's what I did:

1. Insert a new module.
2. Stick this code in it:


Public Sub CloseBook()

Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
ThisWorkbook.Close

End Sub


3. In "ThisWorkook" object, stick in this code:


Private Sub Workbook_Open()
CloseBook
End Sub


This doesn't really answer your question, but I don't know why having just the code in the Open event didn't trigger the macro warning.

PS:: I just tested your code by opening the workbook from another workbook. It works fine. I've not been able to duplicate my error of not getting the macro warning to come up when the code is in the Workbook_Open module. What is your code for opening this workbook from another workbook? This might be the real culprit and not this section of code. It could also explain why no one has been able to solve your problem.

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-04-22 07:45 ]
Mark O'Brien is offline   Reply With Quote
Old Apr 22nd, 2002, 08:48 AM   #8
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Sorry Tim, I just read your post (again) and I realised I pretty much just duplicated what you said. I must not have read it properly the first time.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 22nd, 2002, 10:33 AM   #9
Tim Francis-Wright
Board Regular
 
Join Date: Feb 2002
Posts: 74
Default

I think I know (part of) the problem.

Using
Public Sub Workbook_Open,
I got the close command to fire when opening the file from the File menu, even with another workbook open. I still can't get the close command to fire if I open the
workbook within VBA.

A workaround for that behavior might be to remove the Close command from the Workbook_Open
code and put it elsewhere. For example, if
MySub in some other workbook is the sub that
opens up this ephemeral workbook, then that
code could do the closing, too.

When there is another file open,
__________________
"Interfere? Of course we should interfere! Always do what you're best at, that's what I say!" -- The Doctor, Nightmare of Eden
Tim Francis-Wright is offline   Reply With Quote
Old Apr 22nd, 2002, 11:01 AM   #10
Michael Pettinicchi
Board Regular
 
Join Date: Apr 2002
Posts: 53
Default

Ok you guys. You've got me.

I've tried everything you said (I think) and it DOES NOT WORK!

Here is exactly what I've got.

In WK1.XLS, Sheet 1, I have a button which when clicked executes the following from the Sheet 1 VBA code which is:

Private Sub CommandButton1_Click()
Workbooks.Open "WK2.XLS"
ActiveWorkbook.RunAutoMacros xlAutoOpen
End Sub

In WKB.XLS, Sheet 1 VBA code, I have:

Public Sub CloseBook()
Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
ThisWorkbook.Close
End Sub

In WKB.XLS, ThisWorkbook VBA code, I have:

Private Sub Workbook_Open()
CloseBook
End Sub

I believe this is exactly as Mark said and all I get when executing WKB gets opened is a compile error message "Sub or Function not defined."

I tried Tim's way, but it doesn't work. I don't get an error message but WK2.XLS wont close automatically. What I might be missing is this "Auto" business. Am I supposed to place my sub in a special place. I tried it in Sheet 1 VBA and in ThisWorkbook VBA pages.

Best regards from
EXCELently Frustrated Michael
Michael Pettinicchi 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 04:13 PM.


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