PDA

View Full Version : Custom Macro Buttons - Word 2007



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.

Frosty
04-15-2011, 07:12 AM
You've got the name of the procedure, but is your code module named "MySCMacros"? Or did you leave it "Module1"?

Janine
04-15-2011, 08:59 AM
Hi Abdullah
I suggest you redownload Greg's code as it works fine. I just took a download and ran in 2010.
Try "enable all macros" turned on.
Did you enable Developer Tab (if it is not).

Abdullah
04-15-2011, 10:55 AM
The Developer Tab is enabled.
As is the "enable all macros option".
I just took the code from Greg's site again, but still get the same issue.

All the macros run when selected from the macro list, but I still get "The macro cannot be found or has been disabled because of your Macro security settings" when I attempt to do either the "My number 1 macro" or "Autotext Complete" from the right click option. Add new comment does work from the right click.

Unfortunately I'm still at a loss.

Frosty
04-15-2011, 11:03 AM
Why don't you post a document with the code. "Greg's site" may be useful for others, but I've posted trying to help and you neither responded to my question nor provided a link of where you're getting your code. Kinda hard to help ;)

However, there are but two possibilities here:
1. You're trying to run the exact same code project other people are running, and getting errors that others aren't getting. If this is the case: there is a setting you do not have correct on your machine.

2. You've adapted someone else's code, but missed a step. If this is the case, you should post your code project here.

As a further explanation on the code module question... you don't necessarily need the following:
.OnAction = "MySCMacros.MyInsertAutoText"
... instead you could use...
.OnAction = "MyInsertAutoText"

The text in front of the period is the name of the code module, whereas the text after the period is the subroutine. That would explain why you see "MyInsertAutoText" in the list of macros, but can't run it from a code-generated right-click button... if your MyInsertAutoText subroutine is in a code module named "MyOtherCodeModule" instead of "MySCMacros"

Make sense?

Janine
04-16-2011, 12:03 AM
Hi Frosty, sorry :cloud9:
The addin code is on this page and the file can be downloaded from bottom of page.

The only other thing is perhaps Trusted Locations needs to be set if the addin is not in a trusted location.

http://gregmaxey.mvps.org/Customize_Word2007_SC_Menu_Programatically.htm

Janine

Abdullah
04-22-2011, 08:49 AM
Janine,

You're right, it just required me downloading a fresh copy. I also did not read the "inconsistent issue" paragraph at the bottom. A fresh copy and actually reading that paragraph helped. I think I was falling prey to the issue the paragraph explained.

Thank you both, Janine and Frosty, for the input, and time you spent.

Janine
04-22-2011, 11:57 AM
Hi Abdullah

That's great. Good luck with your project.
Janine:cloud9: