0

I'm wondering if someone can help me out with a VBA issue -- I haven't written VBA in over 20 years and I'm stuck.

I've created a ribbon button and I need the button greyed out if a word document is open from a local drive or file share. I need the button active if the document is open from SharePoint or OneDrive.

Dim Rib As IRibbonUI
Public MyTag As String

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

End Sub

Sub GetEnabledMacro(control As IRibbonControl, ByRef Enabled)
If MyTag = "Enable" Then
    Enabled = True
Else
    If control.Tag Like MyTag Then
        Enabled = True
    Else
        Enabled = False
    End If
End If
End Sub

Sub RefreshRibbon(Tag As String)
MyTag = Tag
If Rib Is Nothing Then
    MsgBox "Error, Save/Restart your workbook" & vbNewLine & _
    "Visit this page for a solution:     Else
    Rib.Invalidate
End If
End Sub

Sub EnabledAllControls()
'Enable all controls
Call RefreshRibbon(Tag:="*")
End Sub

Sub DisableAllControls()
'Disable all controls
Call RefreshRibbon(Tag:="")
End Sub

Then I'm using this if statement to test file location. It just doesn't fire?

Sub AutoOpen()
If InStr(ActiveDocument.Path, "http") = 1 Then
Call EnabledAllControls
Else

Call DisableAllControls

End If
End Sub

I've been working on this for over a week and I'm under the gun to get it done.

1 Answers1

0

The following is untested, but I've made a lot of ribbons and it feels right. It's all code that you had with what I think are the unnecessary parts deleted:

Dim Rib As IRibbonUI

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

Sub GetEnabledMacro(control As IRibbonControl, ByRef Enabled)
Enabled = If InStr(ActiveDocument.Path, "http") = 1
End Sub

Sub AutoOpen()
If Rib Is Nothing Then
    MsgBox "Error, Save/Restart your workbook" & vbNewLine & _ 
    "Visit this page for a solution:"
Else
    Rib.Invalidate
End If
End Sub

Basically, you just need the ribbon object (check), the Event that triggers the setting of the state (check) and the logic to determine the state (check). You don't need a global variable.

Of course your Ribbon XML must assign Sub GetEnabledMacro to the control's GetEnabled property.

Doug Glancy
  • 27,214
  • 6
  • 67
  • 115