如何通过VBA(Excel)在编辑框功能区上设置文本

时间:2013-09-06 15:04:22

标签: excel vba excel-vba ribbon

如何在功能区编辑框中设置文本?我在互联网上找不到它:/

我只能找到点击事件的例子,但没有关于从Sub设置文本的内容。

所以,例如,我想要这样的事情:

Sub settingText()
   editboxname = "my text"
end sub

2 个答案:

答案 0 :(得分:10)

我在此链接中找到的解决方案:http://www.shulerent.com/2011/08/16/changing-the-value-of-an-editbox-office-ribbon-control-at-runtime/

这是我测试的一个例子,它运作良好:

'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>

答案 1 :(得分:1)

自从这个答案发布以来已经有一段时间了,并且看起来是功能区的行为的近期变化,这意味着发布的原始答案可能不再是解决方案。为了记录,我使用Excel 2013进行了一些在Braulio回答后更新的更新。

不同之处在于功能区上的Invalidate和InvalidateControl的行为方式与以前相同。这意味着InvalidateControl不会在editBox上调用getText回调。我用Invalidate替换了InvalidateControl调用(因此强制在整个功能区上重新绘制),这确实会按预期触发回调。

所以这里是我的文件名/浏览按钮解决方案的代码(注意我已经包含了用于在非常隐藏的工作表上缓存功能区UI参考的额外代码,以便在开发过程中重置&#39;使色带无法进入)。

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

为了记录,这里是我在这里处理的两个对象的XML:

<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" />