PDA

View Full Version : Keep custom toolbar with Outlook 2007 project?



clhare
04-18-2013, 09:50 AM
I have created some macros in Outlook 2007, put them on a custom toolbar, and saved the project. I copied the file elsewhere and resaved the project without the macros and toolbar so I could see what would happen when I copied it back.

I found that the macros come back in, but the toolbar does not. Is there a way to keep the toolbar with the project? Is it possible to create the toolbar or custom menu in the VB Editor so every time Outlook is opened, the toolbar or menu is there? I don't have access to Visual Studio or anything like that. The only thing I have is the Custom UI Editor, but I don't think that will work with Outlook.

Thanks!

Cheryl

skatonni
04-26-2013, 07:53 PM
Try expanding on this example. If you figure out how to generate more than one button please post the code here.

http://www.vboffice.net/sample.html?mnu=2&lang=en&smp=16&cmd=showitem

clhare
04-29-2013, 12:52 PM
Using the link above, I was able to add one button that will run the macro attached to it, but I haven't figured out how to add additional buttons.

I have also found that the following code does add a new toolbar with 4 buttons on it. When I click on the first 3 buttons nothing happens, but when I click on the 4th button, all 4 macros run! Can anyone tell me how to fix the code below so that each macro will run when it's button is clicked on?

Sub Application_Startup()

Dim oActExp As Outlook.Explorer
Dim cmdBar As CommandBar
Dim cmdBtn As CommandBarButton

Set oActExp = Outlook.ActiveExplorer

Set cmdBar = Application.ActiveExplorer _
.CommandBars _
.Add(Name:="My Macros", _
Position:=msoBarTop, Temporary:=True)

Set cmdBtn = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn.OnAction = "test1"
cmdBtn.Style = msoButtonCaption
cmdBtn.Caption = "Test1"

Set cmdBtn = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn.OnAction = "test2"
cmdBtn.Style = msoButtonCaption
cmdBtn.Caption = "Test2"
cmdBtn.BeginGroup = True

Set cmdBtn = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn.OnAction = "test3"
cmdBtn.Style = msoButtonCaption
cmdBtn.Caption = "Test3"
cmdBtn.BeginGroup = True

Set cmdBtn = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn.OnAction = "test4"
cmdBtn.Style = msoButtonCaption
cmdBtn.Caption = "Test4"
cmdBtn.BeginGroup = True

cmdBar.Visible = True

End Sub

Sub Test1()

MsgBox "This is test 1."

End Sub

Sub Test2()

MsgBox "This is test 2."

End Sub

Sub Test3()

MsgBox "This is test 3."

End Sub

Sub Test4()

MsgBox "This is test 4."

End Sub

Cheryl

skatonni
04-29-2013, 06:00 PM
Seems to me you are overwriting.
Instead of repeating cmdBtn use four different names.
cmdBtn1, cmdBtn2, cmdBtn3 and cmdBtn4.

clhare
04-30-2013, 07:47 AM
Oops! I've messed up when I made the code generic. I've fixed the Application_Startup code as shown below and nothing at all happens now when I click on any of the 4 buttons. It appears that I am missing the connection between clicking on the button and the macro that is supposed to do something. "OnAction" isn't making that connection.

Sub Application_Startup()

Dim oActExp As Outlook.Explorer
Dim cmdBar As CommandBar
Dim cmdBtn1 As CommandBarButton
Dim cmdBtn2 As CommandBarButton
Dim cmdBtn3 As CommandBarButton
Dim cmdBtn4 As CommandBarButton

Set oActExp = Outlook.ActiveExplorer
Set cmdBar = Application.ActiveExplorer _
.CommandBars _
.Add(Name:="My Macros", _
Position:=msoBarTop, Temporary:=True)

Set cmdBtn1 = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn1.OnAction = "Test1"
cmdBtn1.Style = msoButtonCaption
cmdBtn1.Caption = "Test1"

Set cmdBtn2 = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn2.OnAction = "Test2"
cmdBtn2.Style = msoButtonCaption
cmdBtn2.Caption = "Test2"
cmdBtn2.BeginGroup = True

Set cmdBtn3 = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn3.OnAction = "Test3"
cmdBtn3.Style = msoButtonCaption
cmdBtn3.Caption = "Test3"
cmdBtn3.BeginGroup = True

Set cmdBtn4 = cmdBar.Controls.Add(Type:=msoControlButton)
cmdBtn4.OnAction = "Test4"
cmdBtn4.Style = msoButtonCaption
cmdBtn4.Caption = "Test4"
cmdBtn4.BeginGroup = True
cmdBar.Visible = True

End Sub
Cheryl

skatonni
05-01-2013, 05:42 PM
Could you have duplicates of Test1, Test2, Test3 & Test4?

clhare
05-02-2013, 04:27 AM
No, there are no duplicates. Everything is in the ThisOutlookSession module. If I run the Application_Startup macro, the toolbar is added, but nothing happens when I click on the buttons. If I run the 4 test macros using Tools > Macros..., they work. So there's something wrong with the way I am connecting the macros to the toolbar buttons, but I can't figure out what it is.

Cheryl