PDA

View Full Version : Solved: WORD 2010 Custom UI Callbacks not working



EdHunter
11-27-2012, 02:06 PM
I have created a Word template which I am loading as a add-in with custom UI ribbon buttons,

These buttons SHOULD call macros which are in the template however none of them "fire" - I have tried everything I can think of and have spent a couple of hours trawling the web to no avail :banghead::banghead::banghead::banghead::banghead:

Attached is the document (in zip file)

Two of the Macros are "addprotection" &"removeprotection" (these are the easiest to test) - All they do is enable document protection with a password.

If anybody can help me to get thse to work I would appreciate it.

The callback code is as follows (this is within a module):



'Callback for Addprotection onAction
Sub Addprotection(control As IRibbonControl)

End Sub

'Callback for RemoveProtection onAction
Sub RemoveProtection(control As IRibbonControl)

End Sub

'Callback for MergeSaveSplit onAction
Sub MergeSaveSplit(control As IRibbonControl)

End Sub

'Callback for SplitandSave onAction
Sub SplitandSave(control As IRibbonControl)

End Sub

fumei
11-27-2012, 03:06 PM
There are problems with your file.

That being said, if the code you posted is accurate - "
The callback code is as follows (this is within a module)

then it is no surpriase they do not "fire". There is nothing there TO fire. You may as well have:
Sub GiraffeCoffeeDrinker(oDoc as Document, Elephants as Elephant)
End Sub

1. There are NO instructions in the procedure...tell me to do nothing, and that is what I will do.

2. They have parameters, so they need to be called from some OTHER procedure to fire anyway.

EdHunter
11-27-2012, 03:52 PM
Hi Fumei, You have said there are problems with my file - Please could you elaborate on what you are meaning (I have attached a .Docm version rather than the Zipped Dotm file I submitted originally 9158) - If it is problems which are stopping you opening it etc then if I know what they are I can try and sort them out. I get no error messages etc when I open either file.

In terms of your response to my code I feel I was obviously not specific enough in my original post:

The VBA I posted was the "bare bones" - these are the callbacks that the "Custom UI editor" program gave me - I have tried the following to get them to work:

I have tried to call my macros within these for example:

'Callback for Addprotection onAction
Sub Addprotection(control As IRibbonControl)

Call macro1

End Sub
I have tried to place my macro code directly into the procedures:

'Callback for Addprotection onAction
Sub Addprotection(control As IRibbonControl)

On Error GoTo leave

Sections = ActiveDocument.Sections.Count

For x = 1 To Sections

ActiveDocument.Sections(x).ProtectedForForms = True
Next x


ActiveDocument.Protect Password:="randompasswordhere", NoReset:=False, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False


leave: Exit Sub

End Sub

I have done both the above steps from within modules & also by putting the callbacks into the "ThisDocument" rather than a module

I have tried to make all routines (In various combinations) Public (As in "Public Sub" rather than just "Sub")

If I create a custom ribbon tab and insert Macros into this they work fine - the problem is trying to do this as part of a add-in.

The XML code I am using to create the custom Ribbon tab is as follows:


<mso:customUI xmlns:x1="http://schemas.microsoft.com/office/2009/07/customui/macro" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
<mso:ribbon>
<mso:qat/>
<mso:tabs>
<mso:tab id="mso_c2.1FF68E" label="Useful Stuff" insertBeforeQ="mso:TabAddIns">
<mso:group id="mso_c3.1FF68E" label="New Group" autoScale="true">
<mso:button idQ="x1:Addprotection" size="large" label="Add Protection" imageMso="Lock" onAction="Addprotection" visible="true"/>
<mso:button idQ="x1:RemoveProtection" size="large" label="Remove Protection" imageMso="AdpPrimaryKey" onAction="RemoveProtection" visible="true"/>
<mso:button idQ="x1:MergeSaveSplit" size="large" label="Merge &amp;&amp; Split" imageMso="EquationOptions" onAction="MergeSaveSplit" visible="true"/>
<mso:button idQ="x1:SplitandSave" size="large" label="Split Premerged" imageMso="QueryUnionQuery" onAction="SplitandSave" visible="true"/>
</mso:group>
</mso:tab>
</mso:tabs>
</mso:ribbon>
</mso:customUI>
Any further help would be appreciated.

gmaxey
11-27-2012, 05:30 PM
You have several issues involved here. First as Fumei mentioned, your callbacks are empty. Even if they did fire, who would know it?

Change you callback to this:

Sub ButtonOnAction(control As IRibbonControl)
MsgBox control.ID
Select Case control.ID
Case "Addprotection"
PasswordProtect.Addprotection
Case "RemoveProtection"
PasswordProtect.RemoveProtection
Case "SplitandSave"
'Call as appropriate.
Case "MergeSaveSplit"
'Call as appropriate.
End Select
End Sub


Change all of you onAction attributes in the XML script to this:

"Callbacks.ButtonOnAction"

Get rid of the iqQ="xl.whatever"
and replace with id="whatever"

for all of the buttons. idQ does not work with button commands.

EdHunter
11-27-2012, 05:50 PM
Hi Greg, That's great got it all to work now.

Before seeing your response I actually tried to use the guide on your website

http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html

I got it all sorted (or so I thought) However there was something in there which was causing the Macros to be blocked (even when setting all security as low as possible) - But this code has worked great.


In regards to my XML - Thats because I created the custom ribbon then exported it and pulled the XML out of the export file. - Suppose its the same as recording Macros - You always end up with spacefiller/It never runs right again.

Don't know how many more languages I can fit in my head what with VBA, HTML, XML - I have a hard enough time with English and I am from England. :rofl:

gmaxey
11-27-2012, 06:00 PM
Ed,

Check your Word Options>Advanced>General and made sure that Show Add-in interface errors is checked. Sometimes an unflagged error regardless of how small can tear down the whole house of cards.

Paul_Hossler
11-27-2012, 06:58 PM
1. You can remove the <mso:qat/> - the QAT can only be customized in startFromScratch mode

2. You have a module called MergeSaveSplit and a sub called MergeSaveSplit. I've always found that's a good way to confuse VBA.

3. For something like this, do you really need to use 2 namespaces?

4. I like to use Option Explicit at the top of all modules

5. I don't think Word has the 'add in' the way Excel has .xlam's. I just put such Tool templates in one of the Word start up folders. I usually use C:\Users\<userid>\AppData\Roaming\Microsoft\Word\STARTUP\Tools.dotm


Paul