View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Greg Wilson[_3_] Greg Wilson[_3_] is offline
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.


.