PDA

View Full Version : duplicate groups on my custom tab :-(



HelenT
06-08-2016, 04:19 AM
Hi,

I have used the custom UI editor to add a custom group to a shared custom tab in normal.dotm.

If I open Word, then edit, save and close Word (and consequently the new document Doc1.docx which was presented to me upon opening Word) WITHOUT using any of the custom functionality on the custom tab, the new file re-opens successfully. (Presumably the new file is based on normal.dotm?).

If I open Word, then edit, use some of the custom functionality on the custom tab, save and then close Word, when I reopen the new file Doc1.docx, a duplicate custom group appears on the custom tab. This appears to be because the onLoad routine (corresponding to the normal.dotm custom UI XML) gets run twice ...

Any idea how I can stop this happening, please?

Many thanks, Helen.

SamT
06-08-2016, 06:33 AM
I don't see anything wrong with your code. :rofl: ... :devil2:

HelenT
06-08-2016, 07:43 AM
I don't see anything wrong with your code. :rofl: ... :devil2:

Here is the XML code (had to remove the URL from customUI xmlns as VBA Express would not allow me to post it ...):

<customUI xmlns="schemas.microsoft.com/office/2006/01/customui" xmlns:x="myNameSpace"onLoad="RibbonControl.Onload">
<ribbon startFromScratch="false">
<tabs>
<tab idQ="x:CustomTab"label="Transcription">
<group id="customGroup"label="Templates">
<dropDown id="attachTemplate"label="Attach template"
getItemCount="RibbonControl.GetItemCount"
getItemLabel="RibbonControl.GetItemLabel"
getSelectedItemIndex="RibbonControl.GetSelectedItemIndex"
onAction="RibbonControl.AttachTemplate" >
</dropDown>
</group>
</tab>
</tabs> </ribbon
</customUI>


And now the VBA which is contained within its' own module in normal.dotm:


Option Explicit

Public myRibbon As IRibbonUI



Sub Onload(ribbon As IRibbonUI)
'Creates a ribbon instance for use in this project
Set myRibbon = ribbon
End Sub


'Callback for DropDown GetItemCount
Sub GetItemCount(ByVal control As IRibbonControl, ByRef count)
'Tell the ribbon to show 5 items in the dropdown
count = 5
End Sub


'Callback for DropDown GetItemLabel
Sub GetItemLabel(ByVal control As IRibbonControl, Index As Integer, ByRef label)
'This procedure fires once for each item in the dropdown. Index is _
received as 0, 1, 2, etc. and label is returned.
label = Choose(Index + 1, "--", "Giant Print", "bookx", "magx", "chess")
End Sub


'Callback DropDown GetSelectedIndex
Sub GetSelectedItemIndex(ByVal control As IRibbonControl, ByRef Index)
'This procedure is used to ensure the first item in the dropdown is selected _
when the control is displayed
Select Case control.ID
Case Is = "attachTemplate"
'Index = Index
Case Else
'Do nothing
End Select
End Sub


'Callback for DropDown onAction
Sub AttachTemplate(ByVal control As IRibbonControl, selectedID As String, selectedIndex As Integer)
Dim DummyVar As Integer
Select Case selectedIndex

Case 0 '--
'do nothing
Exit Sub

Case 1 'Giant Print
' taken from sub gwAddGPTemplate
' On Error GoTo errHandler
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookgp.dot"
End With
MsgBox ("Giant Print template attached!")
Exit Sub

Case 2 'bookx
' On Error GoTo errHandler
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookx.dot"
End With
MsgBox ("bookx template attached!")
Exit Sub

Case 3 'magx
' On Error GoTo errHandler
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\magx.dot"
End With
MsgBox ("magx template attached!")
Exit Sub

Case 4 'chess
' On Error GoTo errHandler
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\chess.dot"
End With
MsgBox ("chess template attached!")
Exit Sub

End Select

errHandler: 'from sub gwAddGPTemplate
If Err.Number = 4248 Then
Documents.Add
End If

End Sub

SamT
06-08-2016, 08:26 AM
I am not sure, but try changing

Set myRibbon = ribbon
To

If myRibbon Is Nothing then Set myRibbon = ribbon

Also in the Doc before close sub set myRibbon = Nothing

Paul_Hossler
06-08-2016, 07:29 PM
Since it appears that your custom tab is only used to attach a different template to a document, what happens if you don't share the custom XML, but only


1. leave it in Normal.dotm or

2. put it in a separate dotm in C:\Users\userid\AppData\Roaming\Microsoft\Word\STARTUP?

Something like



<customUI xmlns="schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonControl.Onload">
<ribbon startFromScratch="false">
<tabs>
<tab id="CustomTab" label="Transcription">
<group id="customGroup" label="Templates">
<dropDown id="attachTemplate" label="Attach template"
getItemCount="RibbonControl.GetItemCount"
getItemLabel="RibbonControl.GetItemLabel"
getSelectedItemIndex="RibbonControl.GetSelectedItemIndex"
onAction="RibbonControl.AttachTemplate" >
</dropDown>
</group>
</tab>
</tabs> </ribbon
</customUI>

HelenT
06-15-2016, 02:44 AM
Hi Sam, Thanks for your advice. Unfortunately the above doesn't help :-( Cheers, Helen.

HelenT
06-15-2016, 02:49 AM
Hi Paul,

Although the custom tab is currently used to only attach templates, my plan is to allow other templates (when attached) to add other functionality to the custom tab so I do really need to share the XML for the tab.

I've tried moving it to a separate template (startup.dotm) in the START UP folder and this seems to have solved the problem!!

Presumably this is because startup.dotm only gets opened/run once during any Word session? Whereas, presumably normal.dotm gets opened/run at start up and then again whenever a document based upon it is opened?

Thanks, Helen.