Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > VBA excel toolbar button


Reply
 
Thread Tools Display Modes
  #1  
Old 01-04-2003, 03:35 PM
Viv
Guest
 
Posts: n/a
Default VBA excel toolbar button


Hi everyone, and thanks for reading this!

I have recently started to learn VB and VBA and am nearing completion of my first mini prog in VBA with excel. I have come to a halt at one little problem. I would like the prog to automatically add a new tollbar item when the project is opened but do not now how to do this. The program is a simple payroll system and just needs a launch point from the main excel toolbar so that the user can access the user forms.

I hope that makes sense

Thanks again

Viv
Reply With Quote
  #2  
Old 01-05-2003, 06:20 AM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

Look up the 'Event' topic for the Workbook object in the VB help, and run a board search on 'commandbar'...
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #3  
Old 01-05-2003, 11:00 AM
Viv
Guest
 
Posts: n/a
Default

ok, bit of a dead end that one

found an article with this
****************************************************

The code below will help you to create a toolbar and button. You'll need to modify it to suit your needs. Toolbar buttons (and menu buttons) have to have a macro to call, so you'll need to create a macro that will be run when the user clicks on the button that you created. Ideally, you'd want to call this procedure from the Workbook_Open procedure.



VB:--------------------------------------------------------------------------------Private Sub AddToolBar()

Dim cmdbar As CommandBar
Dim CmdBtn1 As CommandBarButton

Set cmdbar = CommandBars.Add(Name:="MyToolbar", Position:=msoBarTop, Temporary:=True)
cmdbar.Visible = True

With cmdbar
.Left = CommandBars("Standard").Width
.RowIndex = CommandBars("Standard").RowIndex

Set CmdBtn1 = .Controls.Add(msoControlButton, , , , True)
With CmdBtn1
.Style = msoButtonCaption
.Caption = "MyToolbar" 'The text to appear on the toolbar (i.e. Back)
.TooltipText = "This is my toolbar" 'The tooltip that will appear when the cursor is over that button
.OnAction = "Macro1" 'Whatever macro you want to run when the button is pressed
End With

End With

Set cmdbar = Nothing
Set CmdBtn1 = Nothing

End Sub--------------------------------------------------------------------------------


__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handy
****************************************************

But as yet have been unable to get this to work, will this do what i want it to?? (it's looks like it oughta) i'm sorry, expect this has been asked manytimes before!Where would this code need to go?! Programme is made up of a set of 5 user forms to aid set up and use of payroll spreadsheet, I just need a button for the user to click to access th emain menu of these forms.

I'm still very new to this. thanks for your time

V
Reply With Quote
  #4  
Old 01-05-2003, 12:09 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

I'm helping Viv with this on IRC. We've got the AddToolBar() sub being called from the Workbook_Open, but an error 91 (Object variable or With block variable not set) occurs on this line:

Code:
Set cmdbar = CommandBars.Add(Name:="MyToolbar", Position:=msoBarTop, Temporary:=True)
Hoving the mouse over CommandBars reveals that the object is unset at the point (it is 'Nothing'). Having not worked with Excel much, I don't know why this is or what the solution is. Since the original poster suggested that this be called from Workbook_Open I assumed it would work from there. Are we missing something?

(Original thread here)

TIA
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #5  
Old 01-05-2003, 07:45 PM
max1mum0v3rdr1v's Avatar
max1mum0v3rdr1v max1mum0v3rdr1v is offline
Regular
 
Join Date: Aug 2002
Location: Sultan, WA USA
Posts: 62
Default

If you look at the original code above that Viv posted, you will see that the With statement for cmdbar does not contain an End With. Here is the original statement:

Code:
With cmdbar .Left = CommandBars("Standard").Width .RowIndex = CommandBars("Standard").RowIndex

It should be:

Code:
With cmdbar .Left = CommandBars("Standard").Width .RowIndex = CommandBars("Standard").RowIndex End With

Hope that helps. Good luck!

--Sean
Reply With Quote
  #6  
Old 01-06-2003, 04:40 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

Well, it does contain an End With already, further down (they are nested). The code itself is not a problem, I'm pretty sure it WOULD work, if error 91 wasn't coming up.
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #7  
Old 01-06-2003, 04:52 AM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

The above example, mixes commands used to create a new commandbar with those used to add controls to a commandbar.

Have a read of this lot - http://www.xtremevbtalk.com/sear...der=descending
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #8  
Old 01-06-2003, 05:56 PM
Viv
Guest
 
Posts: n/a
Talking SORTED!!

ok this is what i actually needed-----


Private Sub Workbook_Open()

Dim myButton As CommandBarButton
Set myButton = _
Application.CommandBars("Worksheet Menu Bar").Controls.Add
myButton.Caption = "&Payroll" 'name of mini prog
myButton.Style = msoButtonCaption
myButton.BeginGroup = True
myButton.OnAction = "Showit" 'name of macro


End Sub


Thanks for the help anyway
Reply With Quote
  #9  
Old 03-13-2003, 03:51 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

Hello

I am trying to do this as well, with the first example I get the same error: 91. Then I used this code:

Private Sub Workbook_Open()
Dim myButton As CommandBarButton
Set myButton = _
Application.CommandBars("Worksheet Menu Bar").Controls.Add
myButton.Caption = "Test" 'name of mini prog
myButton.Style = msoButtonCaption
myButton.BeginGroup = True
myButton.OnAction = "Test" 'name of macro

End Sub

You can see the result in the picture. HELP!!!

Each time I click on Test, it creates another button in the bar. Each time I open Excel the buttons are still there, even though the makro isn't on. How do I remove them?
They are also in the wrong place, they should be in the bar below and not in the bar with the pull down menus.

Couldn't you just customize the bar "Standard" or "Visual Basic" and add 5 buttons, draw your own symbols and 1 click does the macro and that's it?

Another nice method would be to create a pulldown menu, with 5 commands, but this Pulldown Menu with its Button should only appear when the appropriate sheet is open.

Thank you very much.
Attached Images
File Type: jpg bild4.jpg (5.9 KB, 25 views)
Reply With Quote
  #10  
Old 03-13-2003, 07:55 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default

Have a look at the two most recent entries in my FAQ
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
  #11  
Old 03-13-2003, 08:31 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

Quote:
Originally Posted by Mill
Have a look at the two most recent entries in my FAQ
Thank you, I'm gonna look at that tomorrow (at work). But how do I remove the 6 "Test" buttons? Just by deleting the file with the according macro in it?

They have to go, each time I open Excel another button is created. One day, there will be thousands of buttons, there is not enough space for all of them.
Reply With Quote
  #12  
Old 03-13-2003, 08:37 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default

Quote:
Originally Posted by Artifi
Thank you, I'm gonna look at that tomorrow (at work). But how do I remove the 6 "Test" buttons? Just by deleting the file with the according macro in it?

They have to go, each time I open Excel another button is created. One day, there will be thousands of buttons, there is not enough space for all of them.
Did you put the code into your Personal Workbook_Open event? If so, you need to remove that macro.

I'm a bit leary about deleting menu items through code. Therefore, I'd suggest just dragging them off by holding the Alt key, clicking on it, and dragging it onto the spreadsheet area.
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
  #13  
Old 03-13-2003, 08:44 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

Quote:
Originally Posted by Mill
Did you put the code into your Personal Workbook_Open event? If so, you need to remove that macro.
Yes I did.
I can remove the whole Excel file, it is emtpy, just that only macro is in it. When I try new things, I try them in a new clean sheet first, before things screw up.

Quote:
I'm a bit leary about deleting menu items through code. Therefore, I'd suggest just dragging them off by holding the Alt key, clicking on it, and dragging it onto the spreadsheet area.
Thank you, tomorrow I will see and tell you. Those files are only at work, not at home.

What does "leary" mean? Is that American english or used everywhere in english speaking countries?
Reply With Quote
  #14  
Old 03-13-2003, 08:50 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default

Quote:
Originally Posted by Artifi
What does "leary" mean? Is that American english or used everywhere in english speaking countries?
Leary, leery, wary, cautious - all mean about the same thing (leary is in the dictionary, but it just says that it's a variant of "leery"). I think it's probably Americanese.

Basically, I've had experiences where more menu items were deleted than what I wanted to delete, which can be pretty scary for someone on a PC at work. Since then, I've found out that you can reset menu bars by right-clicking on it, clicking 'Customize', and then selecting the toolbar from the Toolbars tab and clicking the Reset button.
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
  #15  
Old 03-14-2003, 01:50 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

Hello

I removed the Test buttons and deleted the files, so that was okay. I tried your code, both the commandbar button procedure as well as the pulldown menu procedure. Both make slight problems.

Sub AddToolBar()

Dim cmdbar As CommandBar
Dim CmdBtn1 As CommandBarButton
Dim strTBName As String

strTBName = "MyToolbar"

Instead of Sub AddToolBar this should be in the Open Workbook Procedure like this:

Private Sub Workbook_Open()
Dim cmdbar As CommandBar
Dim CmdBtn1 As CommandBarButton
Dim strTBName As String

strTBName = "MyToolbar"
...
...
End Sub

But then this happens (see jpg), error 424. This line is highlighted:
For Each bar In CommandBars

Where do I put the function and what is the purpose of a function?

The toolbar was put into the upper bar, I prefer it in the lower bar. I changed "Standard" to "Format", that should do it, shouldn't it?


The pulldown program had the following error (see jpg), and this line was highlighted:

Set mnuNew = .Controls.Add(msoControlPopup, , , intPosition, True).

Error roughly translates into: "Index outside the valid area".

Thanks!
Attached Images
File Type: jpg Bild5.jpg (20.1 KB, 17 views)
File Type: jpg Bild6.jpg (6.7 KB, 16 views)
Reply With Quote
  #16  
Old 03-14-2003, 06:08 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default

I'm not sure about your errors because I can't understand German. You should put the line AddToolBar into your Workbook_Open event. The AddToolBar sub and the CheckForToolBar function should go into the Workbook module. Depending on how your toolbars are oriented, changing "Standard" to "Format" should work, I think.
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
  #17  
Old 03-14-2003, 06:35 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

Quote:
Originally Posted by Mill
I'm not sure about your errors because I can't understand German. You should put the line AddToolBar into your Workbook_Open event. The AddToolBar sub and the CheckForToolBar function should go into the Workbook module. Depending on how your toolbars are oriented, changing "Standard" to "Format" should work, I think.
It half worked. The bar appears now, but at the top and not beside the "Format" bar.

What do I do, if I want the bar to appear directly on the screen and not together with the other bars?

And I want at least 6 buttons calling 6 different subs, which bits of code do I repeat?

Sorry for the German.
Reply With Quote
  #18  
Old 03-14-2003, 07:13 AM
Mill Mill is offline
Just another Excel nerd

Retired Moderator
* Guru *
 
Join Date: Feb 2000
Location: Michigan, USA
Posts: 2,624
Default

Okay, try "Formatting" as the name of the toolbar. To make it a floating toolbar, change

Code:
Set cmdbar = CommandBars.Add(Name:=strTBName, Position:=msoBarTop, Temporary:=True)

to

Code:
Set cmdbar = CommandBars.Add(Name:=strTBName, Position:=msoBarFloating, Temporary:=True)

Look in your VBA help file for the Add method of the CommandBars control for more information on that.

Note also that, as of Office 97 (or maybe 2000), all toolbars can be floating or docked, so you can drag them around by their handles any way that you want to.
__________________
"The face of a child can say it all, especially the mouth part of the face." - Jack Handey
Reply With Quote
  #19  
Old 03-14-2003, 09:18 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default

I now changed the prog a bit and renamed the Toolbar. I thought the exitsing toolbar will be changed, but it created another toolbar. Now I want to remove the old toolbar. Sorry.


... oh, it is just done by restarting Excel. That's cool.
Reply With Quote
  #20  
Old 03-17-2003, 05:46 AM
Artifi's Avatar
Artifi Artifi is offline
Centurion
 
Join Date: Mar 2003
Posts: 110
Default Placing the commandbar

You can open up a userform when opening the excel file/sheet with the Workbook Open Event, you can also type in coordinates of where you want the Userform to go.

Aktionen.Show
Aktionen.StartupPosition = 0 'Manual
Aktionen.Left = 200
Aktionen.Top = 200

Can you do the same thing with the commandbar? It should appear floating, but at the bottom and not directly on the screen. Thanks
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->