3

What I'm trying to get working:

  • activate the Text Highlight Color command via a keybinding (not the problem)
  • cycle through 5 of the Default Text Highlight Colors via the same keybinding (or just highlighting the selection, depending on selection.type checked outside the function below)
    • showing the current Color in the corresponding button (built-in ribbon)

Where I'm stuck:

Sub cycleThroughSomeDefaultHighlightColorIndexOptions()
Dim zeNewColor As Long

Select Case Options.DefaultHighlightColorIndex
       Case wdYellow:      zeNewColor = wdBrightGreen
       Case wdBrightGreen: zeNewColor = wdTurquoise
       Case wdTurquoise:   zeNewColor = wdPink
       Case wdBlue:        zeNewColor = wdRed
       Case wdRed:         zeNewColor = wdYellow
       Case Else:          zeNewColor = wdYellow
End Select
Application.Options.DefaultHighlightColorIndex = zeNewColor

End Sub

doesn't throw any error, does change the Application.Options.DefaultHighlightColorIndex,

but doesn't update/show the newly set color on the corresponding (built-in ribbon home tab) button

and just exits out of the Text Highlight Color mode.

  1. Is there a possibility to keep it going?

  2. If it needs to be started again: is there a better way than dirty/interfering sendKeys to call commands like Text Highlight Color?

Update 2019-04-03: In the mean time i found where the IRibbonUI.InvalidateControlMso ControlIDs are listed: Office 2016 Help Files: Office Fluent User Interface Control Identifiers

So after creating a hidden custom ribbon and getting a handle for it on onLoad i could zeWdRibbon.InvalidateControlMso "TextHighlightColorPicker" without any raised error.

But it also doesn't change anything.

Is it possible, that Microsoft just getImages the default imageMso "TextHighlightColorPicker" (yellow) without checking for Application.Options.DefaultHighlightColorIndex , or am I missing something?

blub
  • 1,014
  • 7
  • 18
  • 1
    Take a look at the `IRibbonUI.Invaludate` [method](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.core.iribbonui.invalidate?view=office-pia) - it might be what you are looking for if you are trying to refresh the ribbon. – Victor K Apr 01 '19 at 16:02
  • 1
    I believe you can find your answer in this article along with VBA code (run the form) http://www.wordarticles.com/Shorts/RibbonVBA/RibbonVBADemo.php –  Apr 02 '19 at 14:30
  • When I tested your code snippet I got `Object Required`. Please post everything you need to reproduce the issue. --> https://stackoverflow.com/help/mcve – FreeSoftwareServers Apr 04 '19 at 02:06
  • 1
    It's not just the GUI that is displaying the old color. If I change it and then click the button then the value is changed back to the default of 7. So it seems that the ribbon overwrites the value with something it has stored elsewhere. – HackSlash Apr 04 '19 at 20:44
  • `Application.ScreenRefresh` would otherwise be the answer. – HackSlash Apr 04 '19 at 20:48
  • @HackSlash: If you change the DefaultHighlightColorIndex and then click the BuiltIn button or Application.CommandBars.ExecuteMso "TextHighlightColorPicker": yes, it starts the "highlighting mode" AND SETS IT BACK to what is/was selected/shown in the Ribbon. If you hit CTRL+ALT+H or sendkeys "^%h" instead, the DefaultHighlightColorIndex stays what you set it in code before. – blub Apr 04 '19 at 22:04
  • @blub and so I maintain that the Ribbon is not changed and, in fact, has a different way of tracking what the current highlight color is. You would first need to change that value before you could refresh the view. – HackSlash Apr 04 '19 at 22:40
  • @HackSlash: i fear so, but didn't find any documented other properties for the default highlight color – blub Apr 06 '19 at 16:07

1 Answers1

0

I do something like that, each time gRibbon.Invalidate

#If VBA7 Then
    Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
#Else
    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
#End If

Public gRibbon As IRibbonUI

#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
    Call CopyMemory(objRibbon, lRibbonPointer, LenB(lRibbonPointer))
    Set GetRibbon = objRibbon
    Set objRibbon = Nothing
End Function

Public Sub OnRibbonLoad(ribbon As IRibbonUI)
    Set gRibbon = ribbon
    'SAVE SETTINGS TO REGISTRY
    SaveSetting "POP", "RIBBON", "ribbonPointer", ObjPtr(gRibbon)
End Sub

Public Sub OnActionButton(control As IRibbonControl)
        If gRibbon Is Nothing Then
            Set gRibbon = GetRibbon(GetSetting("POP", "RIBBON", "ribbonPointer"))
        End If
    On Error Resume Next
        gRibbon.Invalidate
    On Error GoTo 0
End Sub
Dmitrij Holkin
  • 1,995
  • 3
  • 39
  • 86
  • This is great for keeping the public ribbon object, which would be set to nothing after an unhandled error, but my problem here seems to be, that there is a second, undocumented kind of Application.Options.DefaultHighlightColorIndex. – blub Apr 06 '19 at 16:05
  • Sets or returns the colour used to highlight text formatted with the Highlight button. `Application.Options.DefaultHighlightColorIndex = wdColorIndex.wdGreen ` – Dmitrij Holkin Apr 06 '19 at 21:23
  • Application.Options.DefaultHighlightColorIndex is completely ignored by iRibbonUI.invalidate AND by Application.CommandBars.ExecuteMso "TextHighlightColorPicker" (and the corresponding button) so there has to be a different property that I didn't find until now. – blub Apr 07 '19 at 13:16
  • @blub if this GetRibbon function actually gives you a handle on the current ribbon then you should be able to modify the ribbon directly. – HackSlash Apr 08 '19 at 15:10