Abdullah
04-14-2011, 12:12 PM
Hello,
I found a bit of code online that I was hoping to work with. It to allows you use custom buttons when you right click in Word 2007.
The first bit makes a change to the normal template, and sets the custom buttons.
Option Explicit
Dim oPopUp As CommandBarPopup
Dim oCtr As CommandBarControl
Sub BuildControls()
Dim oBtn As CommandBarButton
'Make changes to the Normal template
CustomizationContext = NormalTemplate
'Prevent double customization
Set oPopup = CommandBars.FindControl(Tag:="custPopup")
If Not oPopup Is Nothing Then GoTo Add_Individual
'Add PopUp menu control to the top of the "Text" short-cut menu
Set oPopUp = CommandBars("Text").Controls.Add(msoControlPopup, , , 1)
With oPopUp
.Caption = "My Very Own Menu"
.Tag = "custPopup"
.BeginGroup = True
End With
'Add controls to the PopUp menu
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "My Number 1 Macro"
.FaceId = 71
.Style = msoButtonIconAndCaption
'Identify the module and procedure to run
.OnAction = "MySCMacros.RunMyFavMacro"
End With
Set oBtn = Nothing
'Add a Builtin command using ID 1589 (Co&mments)
Set oBtn = oPopUp.Controls.Add(msoControlButton, 1589)
Set oBtn = Nothing
'Add the third button
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "AutoText Complete"
.FaceId = 940
.Style = msoButtonIconAndCaption
.OnAction = "MySCMacros.MyInsertAutoText"
End With
Set oBtn = Nothing
Add_Individual:
'Or add individual commands directly to menu
Set oBtn = CommandBars.FindControl(Tag:="custCmdBtn")
If Not oBtn Is Nothing Then Exit Sub
'Add control using built-in ID 758 (Boo&kmarks...)
Set oBtn = Application.CommandBars("Text").Controls.Add(msoControlButton, 758, , 2)
oBtn.Tag = "custCmdBtn"
If MsgBox("This action caused a change to your Normal template." _
& vbCr + vbCr & "Recommend you save those changes now.", vbInformation + vbOKCancel, _
"Save Changes") = vbOK Then
NormalTemplate.Save
End If
Set oPopUp = Nothing
Set oBtn = Nothing
End Sub
The buttons activate/call macros. They are below.
Sub RunMyFavMacro()
MsgBox "Hello " & Application.UserName & ", this could be the results of your code."
End Sub
Sub MyInsertAutoText()
On Error GoTo Err_Handler
Application.Run MacroName:="AutoText"
Exit Sub
Err_Handler:
Beep
Application.StatusBar = "The specified text is not a valid BuildingBlock name."
End Sub
The macros work on their own, when called through the macro selection. But when using the right click custom button, Word returns a message saying "the macro cannot be found or has been disabled because of your Macro security settings."
The definitely have not been disabled, but I don't know why they can't be found, especially since the names are spelled the same, and they do run.
I believe I have included all the correct libraries, and have the code correct.
Any advice would be greatly appreciated.
I found a bit of code online that I was hoping to work with. It to allows you use custom buttons when you right click in Word 2007.
The first bit makes a change to the normal template, and sets the custom buttons.
Option Explicit
Dim oPopUp As CommandBarPopup
Dim oCtr As CommandBarControl
Sub BuildControls()
Dim oBtn As CommandBarButton
'Make changes to the Normal template
CustomizationContext = NormalTemplate
'Prevent double customization
Set oPopup = CommandBars.FindControl(Tag:="custPopup")
If Not oPopup Is Nothing Then GoTo Add_Individual
'Add PopUp menu control to the top of the "Text" short-cut menu
Set oPopUp = CommandBars("Text").Controls.Add(msoControlPopup, , , 1)
With oPopUp
.Caption = "My Very Own Menu"
.Tag = "custPopup"
.BeginGroup = True
End With
'Add controls to the PopUp menu
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "My Number 1 Macro"
.FaceId = 71
.Style = msoButtonIconAndCaption
'Identify the module and procedure to run
.OnAction = "MySCMacros.RunMyFavMacro"
End With
Set oBtn = Nothing
'Add a Builtin command using ID 1589 (Co&mments)
Set oBtn = oPopUp.Controls.Add(msoControlButton, 1589)
Set oBtn = Nothing
'Add the third button
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "AutoText Complete"
.FaceId = 940
.Style = msoButtonIconAndCaption
.OnAction = "MySCMacros.MyInsertAutoText"
End With
Set oBtn = Nothing
Add_Individual:
'Or add individual commands directly to menu
Set oBtn = CommandBars.FindControl(Tag:="custCmdBtn")
If Not oBtn Is Nothing Then Exit Sub
'Add control using built-in ID 758 (Boo&kmarks...)
Set oBtn = Application.CommandBars("Text").Controls.Add(msoControlButton, 758, , 2)
oBtn.Tag = "custCmdBtn"
If MsgBox("This action caused a change to your Normal template." _
& vbCr + vbCr & "Recommend you save those changes now.", vbInformation + vbOKCancel, _
"Save Changes") = vbOK Then
NormalTemplate.Save
End If
Set oPopUp = Nothing
Set oBtn = Nothing
End Sub
The buttons activate/call macros. They are below.
Sub RunMyFavMacro()
MsgBox "Hello " & Application.UserName & ", this could be the results of your code."
End Sub
Sub MyInsertAutoText()
On Error GoTo Err_Handler
Application.Run MacroName:="AutoText"
Exit Sub
Err_Handler:
Beep
Application.StatusBar = "The specified text is not a valid BuildingBlock name."
End Sub
The macros work on their own, when called through the macro selection. But when using the right click custom button, Word returns a message saying "the macro cannot be found or has been disabled because of your Macro security settings."
The definitely have not been disabled, but I don't know why they can't be found, especially since the names are spelled the same, and they do run.
I believe I have included all the correct libraries, and have the code correct.
Any advice would be greatly appreciated.