Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 11th, 2010, 12:47 PM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strange SaveAs behavior

I am developing an application and have run into a weird problem. Basically it boils down to something given below-

When I close a file, I want it to be automatically saved under a different name.

In ThisWorkbook module, if I have the following code,
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Msgbox "Closing file" 
SaveAs "c:\Newname.xlsm"
End Sub
then it works properly if I use the Close command of the application or the 'x' button of the workbook window.

But if I have the following sub in a module,

Code:
Sub VBA_Close()
    ThisWorkbook.Close
End Sub
and call this, it triggers the previous sub (as confirmed by the message box), and the file closes, but no new file gets created.

Why is this?

My code is not the same as the above. It is an add-in and the workbook to be closed is a different workbook from the add-in, and the second sub corresponds to a RibbonX command. But I am observing the same behavior.

I guess I can include the SaveAs in the second sub instead of relying on it to trigger the event for the first, but then my code will be more complicated.

Thanks.

Shankar
 
Old February 11th, 2010, 11:07 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Shankar

Do you mean to save the .close method is executing on a different workbook?

If that is the case you need to have a look at the module which calls ThisWorkbook.Close. ThisWorkbook is the workbook in which the module resides. If this not you intended then you can use

Workbooks(1).Close

or

Workbooks("Sample.xls").Close etc

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old February 12th, 2010, 02:15 AM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I am doing it the way you have indicated in my add-in. The example I have provided is for illustration only. Thanks.
 
Old February 12th, 2010, 02:22 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Is your problem solved now or are you still facing some issues

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old February 12th, 2010, 02:29 AM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a workaround. But I am just curious to know what's the reason for this behavior.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Behavior With Anchor in XSLT kwilliams XSLT 6 July 21st, 2005 01:52 PM
Strange behavior Listview under XP pavel Pro VB 6 1 June 1st, 2005 05:14 AM
STRANGE behavior..SQL Help skotman Classic ASP Databases 7 June 6th, 2004 02:55 PM
Strange behavior of DateTimePicker? wwz VS.NET 2002/2003 0 February 19th, 2004 06:56 AM
Please help with strange file download behavior! glwatson Classic ASP Basics 0 September 10th, 2003 07:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.