LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default saving toolbar buttons on custom toolbar

Hi Paul,

What happens when you assign a macro to a toolbar button
is that not only is the name of the macro referenced for
the particular button but the workbook of origin as well.
To prove this, add a button to the Worksheet Menu Bar at
the end (far right) and assign it a macro. Then run the
following code:

Sub TestMacroAssignment()
Dim X As Integer
X = Application.CommandBars(1).Controls.Count
MsgBox Application.CommandBars(1).Controls(X).OnAction
End Sub

You will get a message referencing the workbook followed
by the name of the macro that was assigned. If your
workbook is named "Timesheets.xls" then you'll get a
message something like:
'C:\Windows\Desktop\Timesheet.xls'!MyMacro

Remember, toolbars are global to Excel as opposed to a
particular workbook. When you save a workbook under a
different name you DO NOT also create a duplicate series
of toolbars including custom toolbars.

What happens when someone saves the workbook under a
different name (SaveAs) is that Excel reassigns the
workbook references for custom toolbar button macros to
the new workbook. In other words, if you were to now run
the above code with the new workbook open you would now
return a message something like this:
"C:\Windows\MyDocuments\SalesDivTimesheets.xls'!My Macro

After some evil person has done this and closed the new
workbook, if you now come along and open the original
workbook and click on the same button that was added to
the Worksheet Menu Bar, it will return an error message
something like: Cannot find macro "MyMacro" or such.
This is because it is looking for the code in the new
workbook which is closed.

In my experience, this is likely to occur frequently by
people who treat your workbook as a template to crunch the
numbers and then save their work under a different name.
However, if you follow my suggestion, whenever the
original or any duplicate workbooks are opened, the
toolbars and buttons are created anew and the macros are
also assigned anew to each button. Upon closing the
workbooks, they are also deleted automatically. Problem
solved. I suggest you check it out experimentally.

As I said previously, if you have edited the button images
and want to use the customized images, there is also a way
to accomodate this.

Regards,
Greg

-----Original Message-----
Greg - in your last message, you said something I didn't

understand.

When you said:

"if someone saves your workbook under a different name

(using SaveAs) then
all the macro references will link to the new workbook

and will no longer
work for the original version. "

I don't understand what "macro references" are, and how

saving the workbook
under a different name would change anything. All my VBA

procedures are
stored in modules that are included in the workbook. If

you save the
workbook under a different name, both the original

workbook and the new one
each contain the modules with those VBA procedures in

them. And nowhere in
my code do I make any mention of the name of the workbook

file. I do have a
line that uses the "ThisWorkbook" object, but that isn't

affected by the
name of the workbook.

So my expectation is that my VBA procedures will work

just fine regardless
of the name of the workbook. Am I wrong about that?

By the way, thanks for the explanation of attaching the

toolbar to the
workbook. I'm going to test your theory, but it makes

sense, and I think
you're right that it saves only the original version.

I'm going to build a
new toolbar and see if I can overcome the problem that

way.


.



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2003 Custom toolbar problem saving new buttons Karen O Excel Discussion (Misc queries) 6 June 23rd 09 10:42 PM
Buttons in a toolbar Daddio_1962 Excel Discussion (Misc queries) 8 January 15th 08 04:50 AM
custom toolbar looses buttons when closed Charlie G Setting up and Configuration of Excel 2 July 21st 06 07:10 AM
Toolbar buttons Liz Excel Discussion (Misc queries) 6 January 23rd 06 12:10 PM
How can I keep Toolbar Buttons on the toolbar? weberts2 Setting up and Configuration of Excel 1 July 16th 05 04:30 AM


All times are GMT +1. The time now is 06:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"