添加自定义命令并更改 Office 2010 Backstage 视图中的控件格式

Office 可视操作方法

**摘要:**了解如何在新的 Office 2010 Backstage 视图中创建快速命令按钮,以及如何在显示 Backstage 视图时设置控件格式。

上次修改时间: 2015年3月9日

适用范围: Excel 2010 | Office 2010 | Open XML | PowerPoint 2010 | VBA | Word 2010

**发布时间:**2010 年 4 月

**供稿人:**Michael Case,iSoftStone

概述

Microsoft Backstage 视图是 Microsoft Office 2010 中的一项新功能,它将代替传统的"文件"菜单。Backstage 视图为文件管理任务(例如,打开新文件或现有文件、定义文档属性和共享信息)提供一个集中空间。与 Microsoft Office Fluent 功能区类似,可使用 XML 扩展 Backstage 视图以定义结构、组件和回调过程,以便为这些组件提供功能。

本文介绍如何创建特定于文档的自定义的 Backstage 视图。Backstage 视图自定义项包括一个自定义快速命令按钮,并演示如何使用回调过程以基于显示 Backstage 视图时的情况来动态设置一组控件的显示方式。

本文使用 Trang Luu 的自定义 UI 编辑器(该链接可能指向英文页面)简化了将自定义用户界面 (UI) XML 添加到 Microsoft Excel 2010 工作簿的过程。您必须下载并安装自定义 UI 编辑器才能按照本文中提供的步骤进行操作。

编码

本文介绍如何使用 XML 和 VBA 代码的组合来自定义 Microsoft Excel 2010 工作簿的 Backstage 视图。XML 将定义要在 Backstage 视图中显示的一个自定义选项卡和多个控件。VBA 代码将提供在自定义 UI XML 中定义的回调过程所需的功能。

本文包含以下步骤,以演示如何在 Excel 2010 工作簿中创建自定义的 Backstage 视图:

  1. 创建 Excel 工作簿

  2. 添加自定义 UI XML

  3. 添加 VBA 回调代码

  4. 查看自定义 Backstage 视图页

读取

创建新的 Excel 工作簿

在本文中,您将自定义 UI XML 和 VBA 代码添加到新的 Excel 2010 工作簿。您必须将 Excel 2010 工作簿创建为启用宏的工作簿 (.xlsm) 才能支持 VBA 代码。

  1. 启动 Microsoft Excel 2010。

  2. 单击"文件"选项卡。

  3. 单击"另存为"。

  4. 在"保存类型"列表上,选择启用 Excel 宏的工作簿 (*.xlsm)。

  5. 单击"保存"以完成操作。将文档另存为 C:\Temp\BackstageViewCustomCommands.xlsm。

添加自定义 UI XML

自定义 UI 编辑器简化了将自定义 UI XML 添加到在上一个步骤中创建的 Excel 2010 工作簿的过程。

  1. 启动自定义 UI 编辑器。

  2. 在"文件"菜单上,单击"打开"。

  3. 选择在上一过程中创建的 C:\Temp\BackstageViewCustomCommands.xlsm 文件,然后单击"打开"。

  4. 在"插入"菜单上,单击"Office 2010 自定义 UI 部件"。这将在文档中创建 CustomUI14.xml 文件。

  5. 选择"customUI14.xml"文件,然后将以下 XML 复制到该文件中。

    <?xml version="1.0" encoding="utf-8"?>
    <!-- customUI is the root tag of all Fluent UI customizations. -->
    <customUI xmlns="https://schemas.microsoft.com/office/2009/07/customui"
              onLoad="OnLoad">
      <!—The Backstage element defines the custom structure of the Backstage UI. -->
      <backstage onShow="OnShow">
        <!-- The tab element defines the name to display on the 
             Backstage view tab and the controls to display 
             on the page when the user selects the tab. -->
        <tab id="dynamicFormatTab" label="Dynamic Control Format" 
             insertAfterMso="TabInfo">
          <firstColumn>
            <!-- The getStyle and getHelperText callbacks 
                  dynamically set the Work Status Group style 
                  and text. -->
            <group id="workStatusGroup" label="Work Status" 
                   getHelperText="GetWorkStatusHelperText" 
                   getStyle="GetWorkStatusStyle" >
              <primaryItem>
                <button id="sendStatusMailButton" 
                        label="Send Status E-Mail" 
                        imageMso="ReplyAll" />
              </primaryItem>
            </group>
          </firstColumn>
        </tab>
        <!-- This button defines a fast command that displays in the 
             built-in Fast Command task bar above the Backstage view tabs. -->
        <button id="saveAndCloseButton" label="Save &amp;&amp; Close" 
                insertAfterMso="FileSaveAs" imageMso="SourceControlCheckIn" 
                onAction="SaveAndClose" isDefinitive="true" />
      </backstage>
    </customUI>
  6. 在"文件"菜单上,单击"保存"。

  7. 关闭自定义 UI 编辑器

添加 VBA 回调代码

VBA 回调过程将功能添加到上一步骤中通过自定义 UI XML 添加的自定义 Backstage 视图组件。

  1. 启动 Microsoft Excel 2010。

  2. 单击"文件"选项卡。

  3. 单击"打开"。

  4. 打开 C:\Temp\BackstageViewCustomCommands.xlsm 工作簿。

  5. 选择"开发工具"选项卡。

  6. 单击"Visual Basic"。

  7. 在"插入"菜单上,单击"模块"。

  8. 选择"Module1",然后将下面的 VBA 代码复制到文件中。

    ' Reference to the ribbon for refreshing the UI.
    Public processRibbon As IRibbonUI
    
    ' Variable that defines the Work Status group display style.
    Public workStatusGroupStyle As String
    
    ' Called when the Backstage view is first loaded.
    ' Stores a reference to the ribbon for refreshing the UI.
    Sub OnLoad(ribbon As IRibbonUI)
      Set processRibbon = ribbon
    End Sub
    
    ' Called each time the Backstage view is displayed.
    ' Calculates the time since the workbook was last saved and sets the
    ' workStatusGroupStyle variable to the appropriate
    ' BackStageGroupStyle enumeration value.
    Sub OnShow(ByVal contextObject As Object)
      timeSinceLastSave = _
          DateDiff("s", FileDateTime(ActiveWorkbook.FullName), Now)
    
      'If over 20 seconds use the Error style.
      If (timeSinceLastSave > 20) Then
        'This enumeration was renamed after the Office 2010 beta release.
        'For the beta release, use OutSpaceSlabStyle.OutSpaceSlabStyleError.
        workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleError
    
      'If over 5 seconds but under 20 seconds use the Warning style.
      ElseIf (timeSinceLastSave > 5) Then
        'This enumeration was renamed after the Office 2010 beta release.
        'For the beta release, use OutSpaceSlabStyle.OutSpaceSlabStyleWarning.
        workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleWarning
      
      'Otherwise use the Normal style.
      Else
        'This enumeration was renamed after the Office 2010 beta release.
        'For the beta release, use OutSpaceSlabStyle.OutSpaceSlabStyleNormal.
        workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleNormal
      End If
      
      ' Invalidate is called to reset the UI causing all controls
      ' to redisplay and execute all relevant callbacks.
      processRibbon.Invalidate
    End Sub
    
    ' Group Style for the Work Status group.
    ' The workStatusGroupStyle variable is set during the OnShow callback.
    Sub GetWorkStatusStyle(control As IRibbonControl, ByRef returnedVal)
      returnedVal = workStatusGroupStyle
    End Sub
    
    ' Helper Text for the Work Status group.
    ' Text returned is based on the current workStatusGroupStyle value
    Sub GetWorkStatusHelperText(control As IRibbonControl, ByRef returnedVal)
      Select Case workStatusGroupStyle
        Case BackstageGroupStyle.BackstageGroupStyleNormal
          returnedVal = "Still within date completion range."
        Case BackstageGroupStyle.BackstageGroupStyleWarning
          returnedVal = "Warning: Nearing contract completion date."
        Case BackstageGroupStyle.BackstageGroupStyleError
          returnedVal = "Contract completion date has been exceeded."
      End Select
    End Sub
    
    'Called when the Save & Close fast command button is clicked.
    Sub SaveAndClose(control As IRibbonControl)
      'Save the changes in the Active workbook
      ActiveWorkbook.Save
    End Sub
  9. 在"文件"菜单上,单击"保存"。

  10. 关闭 Visual Basic for Applications 编辑器,并返回到工作簿。

查看自定义 Backstage 视图页

若要查看上一示例中定义的自定义 Backstage 视图页和快速命令按钮,您必须通过选择"文件"选项卡来显示 Backstage 视图。在显示 Backstage 视图后,请选择位于内置"信息"选项卡下方的"Dynamic Control Format"选项卡。选择"Dynamic Control Format"选项卡后,您可以查看自定义 Dynamic Control Format Backstage 视图页并与之交互。

"保存并关闭"快速命令按钮将显示在包含内置快速命令按钮的 Backstage 视图中,并位于内置"另存为"快速命令按钮的下方。

图 1. 自定义 Backstage 视图

自定义 Backstage 视图页

读取

扩展 Office 2010Backstage 视图以包含自定义页面和快速命令按钮来支持您自己的需求包含向 Office 文档添加自定义 UI XML 和添加 VBA 代码这两个操作,可为在自定义 UI XML 中指定的回调提供功能。

本文中的示例代码演示如何在显示 Backstage 视图时执行操作。该示例代码还演示如何为经常执行的操作创建自定义快速命令按钮。

初始化自定义 UI

在某些情况下,回调过程所做的更改需要重新显示 Backstage 视图控件。若要重新显示 Backstage 视图控件,请调用 RibbonUI 控件的 Invalidate 方法。

下面的代码示例演示如何在加载自定义 UI 时存储对 RibbonUI 控件的引用。customUI 元素包含 onLoad 属性,在此实例中,该属性用于指定 OnLoad 回调。OnLoad 回调将存储对 RibbonUI 对象的引用,可在回调中使用该引用来重新显示 Backstage 视图控件。

<!-- customUI is the root tag of all Fluent UI customizations. -->
<customUI xmlns="https://schemas.microsoft.com/office/2009/07/customui"
      onLoad="OnLoad">
'Reference to the ribbon for refreshing the UI.
Public processRibbon As IRibbonUI

'Store a reference to the ribbon for refreshing the UI.
Sub OnLoad(ribbon As IRibbonUI)
  Set processRibbon = ribbon
End Sub

在显示 Backstage 视图时执行操作

在某些情况下,您可能需要在显示 Backstage 视图时执行一些操作。例如,根据上次保存工作簿的时间来更改控件的格式。如果最近未保存工作簿,则本示例中的"Work Status"组将通过突出显示为红色或黄色来引起注意。该值取决于自上次保存工作簿经过了多长时间。Backstage 视图组通过使用 getStyle 回调设置其显示样式,该回调将返回下列 BackstageGroupStyle 枚举值之一:

  • BackstageGroupStyleNormal。该组未经任何特殊的可视化处理。

  • BackstageGroupStyleWarning。该组突出显示为黄色。

  • BackstageGroupStyleError。该组突出显示为红色。

下面的自定义 UI XML 示例演示如何指定回调根据显示 Backstage 视图时的情况来动态定义控件在该页面上的显示方式。backstage 元素指定一个 onShow 回调,该回调可估计工作簿保存了多长时间并设置"Work Status"组的样式。每当显示 Backstage 视图时,就会执行 onShow 回调。"Work Status"组指定了 getStyle 和 getHelperText 回调,这些回调将根据 onShow 回调所设置的样式返回适当的样式和文本。

<backstage onShow="OnShow">
  <tab id="dynamicFormatTab" label="Dynamic Control Format" 
       insertAfterMso="TabInfo">
    <firstColumn>
      <!-- The getStyle and getHelperText callbacks 
            dynamically set the Work Status Group style 
            and text -->
      <group id="workStatusGroup" label="Work Status" 
             getHelperText="GetWorkStatusHelperText" 
             getStyle="GetWorkStatusStyle" >
        <primaryItem>
          <button id="sendStatusMailButton" 
                  label="Send Status E-Mail" 
                  imageMso="ReplyAll" />
        </primaryItem>
      </group>

下面的代码示例演示 backstage 元素 onShow 回调(即"OnShow")如何根据上次保存工作簿的时间强调"Work Status"组。OnShow 回调将估计工作簿已保存的时间(以秒为单位)并将 workStatusGroupStyle 变量设置为适当的 BackStageGroupStyle 枚举值。在设置 workStatusGroupStyle 变量后,RibbonUI 对象的 Invalidate 方法将重新显示 Backstage 视图控件。在调用 Invalidate 方法时,将重新执行"Works Status"组的 getStyle 和 getHelperText 回调。

'Called each time the Backstage view is displayed.
'Calculates the time since the workbook was last saved and sets the
' workStatusGroupStyle variable to the appropriate
' BackStageGroupStyle enumeration value.
Sub OnShow(ByVal contextObject As Object)
  timeSinceLastSave = _
      DateDiff("s", FileDateTime(ActiveWorkbook.FullName), Now)

  'If over 20 seconds use the Error style.
  If (timeSinceLastSave > 20) Then
    'This enumeration was renamed after the Office 2010 beta release.
    'For the beta use OutSpaceSlabStyle.OutSpaceSlabStyleError.
    workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleError

  'If over 5 seconds but under 20 seconds use the Warning style.
  ElseIf (timeSinceLastSave > 5) Then
    'This enumeration was renamed after the Office 2010 beta release.
    'For the beta use OutSpaceSlabStyle.OutSpaceSlabStyleWarning.
    workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleWarning
  
  'Otherwise use the Normal style.
  Else
    'This enumeration was renamed after the Office 2010 beta release.
    'For the beta use OutSpaceSlabStyle.OutSpaceSlabStyleNormal.
    workStatusGroupStyle = BackstageGroupStyle.BackstageGroupStyleNormal
  End If
  
  'Invalidate is called to reset the UI causing all controls
  ' to redisplay and to execute all relevant callbacks.
  processRibbon.Invalidate
End Sub

下面的代码示例演示如何使用"Work Status"组的 getStyle 回调、GetWorkStatusStyle 回调、getHelperText 回调和 GetWorkStatusHelperText 回调以根据上次保存工作簿的时间来强调该组。GetWorkStatusStyle 回调将返回 workStatusGroupStyle 变量的 BackstageGroupStyle 值,该值是在显示 Backstage 视图时由 OnShow 回调设置的。GetWorkStatusHelperText 可与 GetWorkStatusStyle 回调并行执行,前者将返回与 BackstageGroupStyle 值对应的文本。

'Group style for the Work Status group.
'The workStatusGroupStyle variable is set during the OnShow callback
Sub GetWorkStatusStyle(control As IRibbonControl, ByRef returnedVal)
        returnedVal = workStatusGroupStyle
End Sub

'Helper text for the Work Status group.
'Text returned is based on the current workStatusGroupStyle value.
Sub GetWorkStatusHelperText(control As IRibbonControl, ByRef returnedVal)
  Select Case workStatusGroupStyle
    Case BackstageGroupStyle.BackstageGroupStyleNormal
      returnedVal = "Still within date completion range."
    Case BackstageGroupStyle.BackstageGroupStyleWarning
      returnedVal = "Warning: Nearing contract completion date."
    Case BackstageGroupStyle.BackstageGroupStyleError
      returnedVal = "Contract completion date has been exceeded."
  End Select
End Sub

创建快速命令按钮

快速命令对于您常用的选项非常有用。本文介绍如何创建一个用于保存当前工作簿并关闭 Backstage 视图的快速命令按钮,以及如何返回工作簿。

下面的自定义 UI XML 示例演示如何定义快速命令按钮。快速命令按钮的 button 元素可定义为 backstage 元素的子级。标签可包含一个 & 号,可使用两个 &amp;&amp; 串联标记添加该符号。将 insertAfterMSO 属性设置为 FileSaveAs 可在内置"另存为"快速命令按钮的下方显示"保存并关闭"按钮。当您单击"保存并关闭"按钮时,OnAction 回调将保存工作簿。将 isDefinitive 属性设置为 True,可在您单击"保存并关闭"按钮时关闭 Backstage 视图并返回到工作簿。

<!-- Backstage defines custom structure of the Backstage UI. -->
<backstage onShow="OnShow">
...
  <!-- This button defines a fast command that displays in the 
       built-in fast commands above the Backstage view tabs. -->
  <button id="saveAndCloseButton" label="Save &amp;&amp; Close" 
          insertAfterMso="FileSaveAs" imageMso="SourceControlCheckIn" 
          onAction="SaveAndClose" isDefinitive="true" />
</backstage>

下面的代码示例演示"保存并关闭"快速命令按钮的 onAction 回调(即 SaveAndClose)如何保存当前工作簿并关闭 Backstage 视图。SaveAndClose 回调仅保存当前工作簿。通过在自定义 UI XML 中设置控件的 isDefinitive 属性,可关闭 Backstage 视图并返回到当前工作簿。

'Called when the Save & Close fast command button is clicked.
Sub SaveAndClose(control As IRibbonControl)
  'Save the changes in the Active workbook.
  ActiveWorkbook.Save
End Sub
观看

观看视频

观看视频(该链接可能指向英文页面)

时长:00:08:37 | 大小:17.5 MB | 类型:WMV

单击以获取代码

获取代码(该链接可能指向英文页面)

浏览

 

关于作者

Michael Case 是 iSoftStone 的高级软件开发人员,他主要负责与客户合作,以创建自定义的软件解决方案。他在使用 Microsoft 技术和 Microsoft .NET Framework 开发桌面解决方案和基于 Web 的解决方案方面均有超过 16 年的经验。