Custom Ribbon working but needs some tuning.

avcape

New Member
Joined
Dec 31, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
I had a problem that starting Excel from Python whacks the Custom ribbon.

I eventually got code that does the job. Starting Excel from Python does not whack the Custom ribbon anymore.
With complements of losing the state of the global IRibbonUI ribbon object
This was written some years ago and seems to me some coding

The code works well on my machine. Quite usable and stable for one's own use but I see some strange things.

Let me just say I know nothing of VBA script nor do I understand the errors. I know other things in the IT field but not code. Hence why I'm here.

Errors I get:
In the Excel VBA editor line 9 text shows red (I dont know why..)

Running the Code in a Libre-Office x64 suite on Win10 x64 I get error
"BASIC syntax error.
Function not allowed within a procedure." (again I dont know why..)

Even more errors come up when running the code in an online compiler. (now I'm even more clueless..)

I have to get this VBA script to work for a personal project I'm working on which is really important to me.
This VBA script must be able to run on Win x64 and x86 platforms and on various Office releases.

All that's needed is some tweaking/tuning a few lines.
I'm asking pretty please if someone can help me to sort the errors.


Thank you in advance.
Much appreciated.


Code:
Option Explicit

Public YourRibbon As IRibbonUI
Public ABCDEFG As String

#If VBA7 Then
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
#Else
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
#End If

Public Sub RibbonOnLoad(ribbon As IRibbonUI)
   ' Store pointer to IRibbonUI
    Set YourRibbon = ribbon
    Sheet1.Range("A1").Value = ObjPtr(ribbon)
    
End Sub


#If VBA7 Then
Function GetRibbon(ByVal lRibbonPointer As LongPtr) As Object
#Else
Function GetRibbon(ByVal lRibbonPointer As Long) As Object
#End If
        Dim objRibbon As Object
        CopyMemory objRibbon, lRibbonPointer, LenB(lRibbonPointer)
        Set GetRibbon = objRibbon
        Set objRibbon = Nothing
End Function


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


Sub RefreshRibbon(Tag As String)
    ITTA = Tag
    If YourRibbon Is Nothing Then
        Set YourRibbon = GetRibbon(Sheets(1).Range("A1").Value)
        YourRibbon.Invalidate
        'MsgBox "The Ribbon handle was lost, Hopefully this is sorted now by the GetRibbon Function?. You can remove this msgbox, I only use it for testing"
    Else
        YourRibbon.Invalidate
    End If
End Sub


'**********************************************************************************
'Examples to show only the Tab with the tag you want with getVisible in the RibbonX.
'**********************************************************************************

Sub DisplayRibbonTab()
'Show only the Tab, Group or Control with the Tag "ABCDEFG"
    Call RefreshRibbon(Tag:="ACDEFG")
End Sub


'Sub DisplayRibbonTab_2()
'Show every Tab, Group or Control with every Tag that start with "My"
    'Call RefreshRibbon(Tag:="My*")
'End Sub

'Sub DisplayRibbonTab_3()
'Show every Tab, Group or Control(we use the wildcard "*")
    'Call RefreshRibbon(Tag:="*")
'End Sub

'Note: in this example every macro above will show you the custom tab.
'If you add more custom tabs this will be different

'Sub HideEveryTab()
'Hide every Tab, Group or Control(we use Tag:="")
    'Call RefreshRibbon(Tag:="")
'End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Tested: This Custom Ribbon code runs fine on any windows and Office release.

Libre Office does not make provision for customUI Ribbon code.
That's why the error "BASIC syntax error. Function not allowed within a procedure."

Thank you.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,755
Messages
6,180,776
Members
452,996
Latest member
nelsonsix66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top