Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a text on a Editbox Ribbon via VBA (Excel)

Tags:

excel

vba

ribbon

How can I set a text in a Ribbon Editbox? I can't find it on internet :/

I just can find examples of click event but nothing about set a text from a Sub.

So for example, I want something like this:

Sub settingText()
   editboxname = "my text"
end sub
like image 321
Braulio Avatar asked Sep 06 '13 15:09

Braulio


2 Answers

The solution I found on this link: http://www.shulerent.com/2011/08/16/changing-the-value-of-an-editbox-office-ribbon-control-at-runtime/

Here is an example that I tested and it worked well:

'Global Variables:
Public MyRibbonUI As IRibbonUI
Public GBLtxtCurrentDate As String

Private Sub OnRibbonLoad(ribbonUI As IRibbonUI)

    Set MyRibbonUI = ribbonUI
    GBLtxtCurrentDate = ""

End Sub

Private Sub ocCurrentDate(control As IRibbonControl, ByRef text)

    GBLtxtCurrentDate = text
    MyRibbonUI.InvalidateControl (control.id)

End Sub

Private Sub onGetEbCurrentDate(control As IRibbonControl, ByRef text)
    text = GBLtxtCurrentDate
End Sub

Public Sub MyTest()
    'Here is an example which you are setting a text to the editbox
    'When you call InvalidateControl it is going to refresh the editbox, when it happen the onGetEbCurrentDate (which is the Gettext) will be called and the text will be atributed.
    GBLtxtCurrentDate = "09/09/2013"
    MyRibbonUI.InvalidateControl ("ebCurrentDate")
End Sub

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnRibbonLoad">
  <ribbon>
    <tabs>
      <tab id="Objects" label="Objects">
        <group id="grp" label="My Group">
          <editBox id="ebCurrentDate" label="Date" onChange="ocCurrentDate" getText="onGetEbCurrentDate"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
like image 157
Braulio Avatar answered Sep 29 '22 19:09

Braulio


It's a little while since this answer was posted, and there looks to be a recent-ish change to the behaviour of the ribbon, which means the original answer posted may not be a solution any more. For the record, I'm using Excel 2013 with some updates that are dated after Braulio's answer.

The heart of the difference is that Invalidate and InvalidateControl on the ribbon don't behave the same way as previously. This means that InvalidateControl does not call the getText callback on the editBox. I replaced the InvalidateControl calls with Invalidate (so forces a re-draw on the entire ribbon), and that does trigger the callback as expected.

So here's the code of my solution for a filename/browse button (note I've included extra code for caching the ribbon UI reference on a very hidden sheet so that resets during development don't make the ribbon inaccessible).

Private sobjRibbon As IRibbonUI
Private strFilename As String

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (destination As Any, source As Any, ByVal length As Long)

Private Function GetRibbon() As IRibbonUI
    If sobjRibbon Is Nothing Then
        Dim objRibbon As Object
        CopyMemory objRibbon, ThisWorkbook.Worksheets("Ribbon_HACK").Range("A1").Value, 4
        Set sobjRibbon = objRibbon
    End If
    Set GetRibbon = sobjRibbon
End Function

'Callback for customUI.onLoad
Sub Ribbon_Load(ribbon As IRibbonUI)
    Set sobjRibbon = ribbon
    Dim lngRibPtr As Long
    lngRibPtr = ObjPtr(ribbon)
    ' Write pointer to worksheet for safe keeping
    ThisWorkbook.Worksheets("Ribbon_HACK").Range("A1").Value = lngRibPtr
    strFilename = ""
End Sub

'Callback for FileName onChange
Sub OnChangeFilename(control As IRibbonControl, text As String)
    strFilename = text
End Sub

'Callback for FileName getText
Sub GetFileNameText(control As IRibbonControl, ByRef returnedVal)
    returnedVal = strFilename
End Sub

'Callback for FilenameBrowse onAction (I'm looking for XML files here)
Sub OnClickFilenameBrowse(control As IRibbonControl)
    Dim objFileDialog As Office.FileDialog

    Set objFileDialog = Application.FileDialog(msoFileDialogFilePicker)

    With objFileDialog
        .AllowMultiSelect = False
        .Title = "Please select the file."
        .Filters.Clear
        .Filters.Add "XML", "*.xml"

        If .Show = True Then
            strFilename = .SelectedItems(1)
            GetRibbon().Invalidate ' Note the change here, invalidating the entire ribbon not just the individual control
        End If
    End With
End Sub

For the record, here's the XML for the two objects I'm dealing with here:

<editBox id="FileName" onChange="OnChangeFilename" screentip="Filename of the XML file to upload" label="XML file name" showImage="false" getText="GetFileNameText" />
<button id="FilenameBrowse" imageMso="ImportExcel" onAction="OnClickFilenameBrowse" screentip="Find the file to upload" label="Browse" />
like image 23
greenbutterfly Avatar answered Sep 29 '22 18:09

greenbutterfly