Solved

Custom Ribbon doesn't refresh

Posted on 2016-09-06
7
59 Views
Last Modified: 2016-09-08
First of all many thanks for take some time to read my question.

I'm new with XML-VBA (actually is my 1st incursion on this)

I've create the following custom ribbon for an Excell addin:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
 <ribbon> 
<tabs>

<!-- Creamos tab para el administrador con sus botones-->

<tab id="customTab" label="BPR Moldeo" insertAfterMso="TabView">

<!-- Creamos grupo de botones 2 "Acceso"-->
				
				<group id="customGroup2" label="Activar sistema" tag="Accede">
					<button id="customButton12" label="Log in" size="large" onAction="sesion" imageMso="ReviewProtectWorkbook" />
					

</group>


<!--Group 0 "Generar BPR"-->
				
				<group id="customGroup0" label="BPRs Moldeo" getVisible="GetVisible" tag="LGGenerar">
					<button id="customButton0" label="Generar" size="large" onAction="Inicio" imageMso="PivotTableInsert" />
					

</group>



<!-- Group Note-->

				<group id="customGroup1" label="By I. Q. Leonel Garcia [QA AL]" getVisible="GetVisible" tag="LGAyuda">
					<button id="customButton11" label="Dudas / Comentarios" size="large" onAction="Envia_Mail" imageMso="FileSendMenu" />
				
				</group>

<!-- Group Exit-->

				<group id="customGroup3" label="Desconexión" getVisible="GetVisible" tag="LGSalir">
					<button id="customButton13" label="Salir" size="large" onAction="Oculta" imageMso="WindowClose" />
				
				</group>

			</tab>
		</tabs>
	</ribbon>
</customUI>

Open in new window


And the following code in my VBA project (not developed for me, as I mentioned I'm new on this)

Module Ribbon

Option Explicit

Public Rib As IRibbonUI
Public MyTag As String

'Callback for customUI.onLoad
Public Sub RibbonOnLoad(ribbon As IRibbonUI)
    Set Rib = ribbon
End Sub

Sub GetVisible(control As IRibbonControl, ByRef visible)
    If MyTag = "show" Then
        visible = True
    Else
        If control.Tag Like MyTag Then
            visible = True
        Else
            visible = False
        End If
    End If
End Sub

Sub RefreshRibbon(Tag As String)
    MyTag = Tag
    'If Rib Is Nothing Then
    '    MsgBox "Error, reinicia Excel"
    'Else
        Rib.Invalidate
    'End If
End Sub

Open in new window


Module Buttons

Sub ShowAllButtons()
'Show All
    Call RefreshRibbon(Tag:="*")
End Sub

Sub HideButtons()
'Hide all with a Tag that start with "LG"
    Call RefreshRibbon(Tag:="Login")
End Sub

Sub Oculta(control As IRibbonControl)
Call HideButtons
End Sub

Open in new window



All works fine when I start Excel, the addin is loaded, the custom "test Tab" is showed, once the login is completed three additional buttons are showed, my problem is that after I press button "Generar"  or  "Dudas/comentarios" the macro assigned to these run  OK and when I press the "Salir" button to hide the buttons but I received the notification that something went wrong (Runtime error 91: Object variable or with block variable not set), I've reviewed and looking for information but I have not find nothing related.

Many thanks in advance for your help.
0
Comment
Question by:Leonel Garcia
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Google for Greg Maxey (MS Word) or Ron de Bruin (MS Excel) both have great examples for custom Ribbons
0
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
Under certain circumstances, when your modules run in an unhandled error or recompiles of code files, the values of module level variables are lost.  So you need error handling in all of your modules methods. Even a

On Local Error Resume Next

Open in new window


is sufficient.

Then you should rethink your approach: Use a generic methods and evaluate the control id's. Cause these methods must be otherwise all defined separately using the correct parameter list. So clean-up your code.

E.g.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
	<ribbon> 
		<tabs>		
			<tab id="tabAdministration" label="BPR Moldeo" insertAfterMso="TabView">			
				<group id="grpAcceso" label="Activar sistema" tag="Accede">
					<button id="btnLogIn" label="Log in" size="large" onAction="RibbonOnAction" imageMso="ReviewProtectWorkbook" />
				</group>
				
				<group id="grpGenerarBpr" label="BPRs Moldeo" getVisible="RibbonGetVisible" tag="LGGenerar">
					<button id="btnGenerar" label="Generar" size="large" onAction="RibbonOnAction" imageMso="PivotTableInsert" />
				</group>
				
				<group id="grpNote" label="By I. Q. Leonel Garcia [QA AL]" getVisible="RibbonGetVisible" tag="LGAyuda">
					<button id="btnDudasComentarios" label="Dudas / Comentarios" size="large" onAction="RibbonOnAction" imageMso="FileSendMenu" />
				</group>
				
				<group id="grpExit" label="Desconexión" getVisible="RibbonGetVisible" tag="LGSalir">
					<button id="btnSalir" label="Salir" size="large" onAction="RibbonOnAction" imageMso="WindowClose" />
				</group>
			</tab>
		</tabs>
	</ribbon>
</customUI>

Open in new window


with

Option Explicit

Private m_Ribbon As IRibbonUI
Private m_Tag As String

Public Sub RefreshRibbon(ATag As String)

  On Local Error Resume Next

  MyTag = Tag
  RibbonInvalidate

End Sub

Public Sub RibbonInvalidate()

  On Local Error Resume Next
  
  If m_Ribbon Is Nothing Then
    MsgBox "D'oh!"
  Else
    m_Ribbon.Invalidate
  End If

End Sub

Public Sub RibbonOnLoad(ARibbon As IRibbonUI)

  On Local Error Resume Next
  
  Set m_Ribbon = ARibbon  
     
End Sub

Public Sub RibbonOnAction(AControl As IRibbonControl)
    
  On Local Error Resume Next
	
  Select case AControl.ID
    Case Is = "btnLogIn"
	  LoginMethod
    Case Is = "btnGenerar"
	  GenerarMethod
    Case Is = "btnDudasComentarios"
	  DudasComentariosMethod
    Case Is = "btnSalir"
	  SalirMethod
  End Select 
  
End Sub

Public Sub RibbonGetVisible(AControl As IRibbonControl, ByRef AVisible)

  On Local Error Resume Next   

  ' Here you can check preconditions (m_Tag) for each button.  
  Select case AControl.ID
    Case Is = "grpGenerarBpr"
	  AVisible = True
    Case Is = "grpNote"
	  AVisible = True
    Case Is = "grpExit"
	  AVisible = True
    Case Else
	  AVisible = False
  End Select 
	
End Sub

Open in new window


and

Option Explicit

Public Sub LoginMethod()

  On Local Error Resume Next
    
  ' Your actual code.
	 
End Sub

Public Sub GenerarMethod()

  On Local Error Resume Next
  
  ' Your actual code.
  
End Sub

Public Sub DudasComentariosMethod()

  On Local Error Resume Next
  
  ' Your actual code.
  
End Sub

Public Sub SalirMethod()

  On Local Error Resume Next

  ' Your actual code.  
  
End Sub

Open in new window


The best resource inclusive tools is: WYSIWYG Ribbon Editor from Gunter Avenius.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
@ste5an  are you actually advocating On Error Resume Next?

Also your comments are confusing (at least they are to me)....

Clean up your code, e.g.

<Ribbon XML>

Open in new window


with

<2 VBA Functions>

Open in new window


Sorry not understanding, perhaps there's a typo?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Leonel Garcia
Comment Utility
Hi ste5an many thanks for your answer, I've reviewed it and definitely is a better approach,  I've set this part as follows:

Public Sub RibbonGetVisible(AControl As IRibbonControl, ByRef AVisible)

  On Local Error Resume Next

  ' Here you can check preconditions (m_Tag) for each button.
  Select Case AControl.ID
    Case Is = "grpGenerarBpr"
      AVisible = False
    Case Is = "grpNote"
      AVisible = False
    Case Is = "grpExit"
      AVisible = False
    Case Else
      AVisible = False
  End Select
    
End Sub

Open in new window


In this way when file opens I only can see login group as I need, my doubt is how can I proceed in order to show the three groups (grpGenerarBpr, grpNote and grpExit)   once the macro assigned to  button btnLogIn finishes?.

Many  thanks in advance for your help.

Best regards
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
Hi ste5an

In response to your recently deleted post, I probably misinterpreted - looked like you were suggesting that the Ribbon XML could be replaced with VBA....

Clean up this <XML> with <VBA>; i.e. I thought replace this <XML> with <VBA>.  Which, obviously is not what you meant.   Guessing the 'with' should have been an 'and'.
0
 
LVL 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 500 total points
Comment Utility
E.g.

Public Sub RibbonGetVisible(AControl As IRibbonControl, ByRef AVisible)

  On Local Error Resume Next

  If m_Tag = "show" Then
    AVisible = True
  Else
    AVisible = (AControl.Tag Like m_Tag)
  End If
    
End Sub

Open in new window


But instead of using tags, you should consider using the id:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
	<ribbon> 
		<tabs>		
			<tab id="tabAdministration" label="BPR Moldeo" insertAfterMso="TabView">			
				<group id="grpAcceso" label="Activar sistema">
					<button id="btnLogIn" label="Log in" size="large" onAction="RibbonOnAction" imageMso="ReviewProtectWorkbook" />
				</group>
				
				<group id="grpGenerarBpr" label="BPRs Moldeo" getVisible="RibbonGetVisible">
					<button id="btnGenerar" label="Generar" size="large" onAction="RibbonOnAction" imageMso="PivotTableInsert" />			
					<separator id="grpGenerarBpr_Separator_1" />
					<button id="btnDudasComentarios" label="Dudas / Comentarios" size="large" onAction="RibbonOnAction" imageMso="FileSendMenu" />
					<separator id="grpGenerarBpr_Separator_2" />
					<button id="btnSalir" label="Salir" size="large" onAction="RibbonOnAction" imageMso="WindowClose" />
				</group>
			</tab>
		</tabs>
	</ribbon>
</customUI>

Open in new window


with

Public Sub RibbonGetVisible(AControl As IRibbonControl, ByRef AVisible)

  On Local Error Resume Next

  If m_Tag = "show" Then
    AVisible = True
  Else
    AVisible = (AControl.ID = m_Tag)
  End If
    
End Sub

Open in new window


and use the group id, instead of the tag value.
0
 

Author Closing Comment

by:Leonel Garcia
Comment Utility
Many thanks,  the direction and clarity of the answer help me to achieve the objective, all works great an now I've learned many new things, many thanks again
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now