PDA

View Full Version : Ribbon Loading Sequence



gmaxey
07-24-2010, 04:35 AM
I am experimenting with a template add-in that will create several variations of a custom tab based on the network username of the user. Since I am working with a stand alone PC without access to a network I similute different usernames with a inputbox and an AutoOpen procedure in the template.

Sub AutoOpen()
pUserName = InputBox("Enter one of the following user names for this demo (Robert Jones, Greg Maxey, Tim Smith, Betty Rogers", "UserName")
End Sub


The username created is then used with a GetVisible callback to determine which custom groups to display:

Sub GetVisible(control As IRibbonControl, RtnVal)
Select Case control.ID
Case "GrpFrontOffice"
Select Case pUserName
Case "Tim Smith", "Mary Allen", "Betty Rogers"
RtnVal = True
Case Else
RtnVal = False
End Select
Case "GrpDrJones"
If pUserName = "Robert Jones" Then RtnVal = True Else: RtnVal = False
Case "GrpCDRMaxey"
If pUserName = "Greg Maxey" Then RtnVal = True Else: RtnVal = False
End Select
End Sub

All seemed to be working great until I closed down Word and restarted Word.

It seems that there is a difference in how the ribbon is loaded when Word is first started.

When I shutdown and restart Word and then open my template the custom tab is already displayed before the AutoOpen procedure fires to define the username. After that name is provided nothing happens.

If I close the document (but not Word) and then reopen the document, the custom tab is not displayed. The AutoOpen macro fires and after a username is provided the correct custom tab configuration is displayed.

To get around this behaviour I altered my AutoOpen procedure as shown:

Sub AutoOpen()
pUserName = InputBox("Enter one of the following user names for this demo (Robert Jones, Greg Maxey, Tim Smith, Betty Rogers", "UserName")
On Error Resume Next
rxIRibbonUI.Invalidate
End Sub


Is there a reason the ribbon loading sequence is different when Word is first started and is there another (or better way) to control how it loads in situations like this?

Sample document attached.

Bob Phillips
07-24-2010, 05:55 AM
I am not sure what problem you are seeing Greg.

WIth the docujment as is, I get prompted for the user once I enable content, both when I double-click the file and start Word, and when I close it and re-open it.

I then commented out the ribbon Invalidate statement and repeated the tests. Again I got prompted for the user and saw their required group.

I must admit, this surprised me as I had expected that the ribbon stuff would load before the AUtoOpen macro, but in my case it did not seem so.

BTW, code like this



If pUserName = "Robert Jones" Then RtnVal = True Else: RtnVal = False


can be simplified as



RtnVal = pUserName = "Robert Jones"

gmaxey
07-24-2010, 08:14 AM
xld,

The steps I take to reveal the issue is:

With the on error and validate lines stetted out:

1. Open Word.
2. Open the template using File>Open
3. The file opens with the Longview Tab and Standard group displayed, then the Input box prompt appears.
4. Enter the user name in the prompt and click OK.
5. The expected group or groups do not display.
6. Close the file.
7. Open the file again using same process.
8. The file opens with the Home tab displayed and the Input box prompt appears.
9. Enter the user name in the prompt and click OK.
5. The expected group or groups display correctly.

I can repeat this behaviour over and over again by completly closing Word.

Using the On Error and invalidate method all works as expected regardless if Word is shut down and restarted.

I'm stumped but can live with the work around.

Thanks for the coding tip.

Bob Phillips
07-24-2010, 10:47 AM
Greg,

Well that is most odd because I don't see that behaviour at all.

I start Word, open the template, I get the prompt, and upon selection of a name, I get the chosen group showing. I even changed the code so that Greg Maxey would get the GM group and the Officxe group, and that worked fine, I saw two groups.

Very odd.

Bob

gmaxey
07-24-2010, 03:23 PM
Bob,

Yes it is odd. I thought perhaps that some of my global add-ins might be at play, but after removing everything from the startup folder and ensuring no add-ins were loaded it still behaves as outlined above.

Wait. Here is another twist. I have Word2003, 2007, and 2010 installed on my PC. The behaviour only occurs with Word2007. Using Word2010 the expected results are seen without the On Error and Invalidate commands even when Word is first started.

Onece again Word perplexes ;-)

Bob Phillips
07-25-2010, 10:53 AM
Well I have 2003 and 2007, but not 2010.

mcramer
07-27-2010, 11:27 AM
I tried it both ways. Loaded the same either way, the dialog box displayed, then the ribbon loaded.

I have Word 2010 only on this computer.

Janine
09-02-2010, 02:34 AM
:cloud9:
Have you tried putting in customxml
for 2007 and 2010 and CustomXML (all three) you just have the last one in the demo.

I loaded this on 2010 - it loads properly first time and then it loads the tab and is stopped by the input box.

I've not tried it on laptop with 2003/2007/2010 all loaded but you do need all three customxml parts as far as I know.

Janine

Janine
09-04-2010, 04:44 AM
Hi Greg,

I put below in and updated your template to 2010 with CustomUI XML and 2010 parts and renamed it as a dotm (template with macros) not a document with macros. Works fine on load and restarting word.

Are you going to list templates in the dynamic dropdowns?

The issue with template corruption is common. I have it all the time when I build templates and I just created a clean profile and test templates in there and they work fine. :cloud9:


Option Explicit
Dim rxIRibbonUI As IRibbonUI

Function pUserName() As String
pUserName = Environ("UserName")
End Function
Sub AutoExec()
'pUserName = InputBox("Enter one of the following user names for this demo (Robert Jones, Greg Maxey, Tim Smith, Betty Rogers", "UserName")
On Error Resume Next
rxIRibbonUI.Invalidate
End Sub

gmaxey
09-04-2010, 05:37 AM
Janine,

I have it working in both 2007 and 2010. The problem seems to be the loading sequence in Word2007 and that is corrected with the On Error and Invalidate statements.

I wouldn't really necessarily want the ribbon to load when Word started but when a document based on the template is opened or created.

The Environ tip is handy. I always forget that one. Thanks.

Janine
09-04-2010, 05:48 AM
No problem (very simple) but it works a treat the Environ.
And it works fine as .dotm I just reran it then.
:cloud9: It is very fast to load both as a dotm and a docm.
Janine