对 Microsoft Office 命令栏进行更多编程(3)

80酷酷网    80kuku.com

  编程CommandBarDocumenter 和 CommandBarControlDocumenter 子例程
在开发命令栏解决方案时,我经常需要获取特定命令栏或命令栏控件的索引、名称或标题。我创建了 CommandBarDocumenter 和 CommandBarControlDocumenter 子例程,以便将所有命令栏和命令栏控件的公共属性记录在给定的 Office 应用程序中。

要运行这些示例,请在 Visual Basic Editor 中将以下代码复制到 Microsoft Office XP 应用程序的代码模块,然后运行以下子例程之一或两者都运行。屏幕出现提示时,请将结果保存为文本文件 (.txt)。这使结果更容易加载到应用程序(例如 Microsoft Excel)中以便查看和过滤。

Public Sub CommandBarDocumenter()

    ' 用途:将当前应用程序中有关所有命令栏的信息
    ' 写入文本文件。
    
    ' 您必须先设置对 Microsoft 脚本运行时的引用
    ' (scrrun.dll) 才能使此代码正确运行。
    
    ' 注意:此代码仅适用于 Microsoft Office XP。
    
    Dim objCommandBar As Office.CommandBar
    Dim strType As String
    Dim strPosition As String
    Dim objFileSaveDialog As Office.FileDialog
    Dim objFSO As Scripting.FileSystemObject
    Dim objTextStream As Scripting.TextStream
    Const SAVE_BUTTON As Integer = -1
    
    Set objFileSaveDialog = Application.FileDialog(msoFileDialogSaveAs)
    
    objFileSaveDialog.Title = "将结果另存为"
    
    ' 用户单击“保存”按钮。
    If objFileSaveDialog.Show = SAVE_BUTTON Then
     
        Set objFSO = New Scripting.FileSystemObject
        Set objTextStream = objFSO.CreateTextFile(objFileSaveDialog.SelectedItems.Item(1))
        

        objTextStream.WriteLine "Name" & vbTab & _
            "Type" & vbTab & _
            "Enabled" & vbTab & _
            "Visible" & vbTab & _
            "Index" & vbTab & _
            "Position" & vbTab & _
            "Protection" & vbTab & _
            "Row Index" & vbTab & _
            "Top" & vbTab & _
            "Height" & vbTab & _
            "Left" & vbTab & _
            "Width"
        
        ' 将下一行替换为:
        ' For Each objCommandBar In Application.ActiveExplorer.CommandBars _
           <- 对于 Outlook
        ' For Each objCommandBar In Application.VBE.CommandBars <- 对于 _
            Visual Basic Editor
        For Each objCommandBar In Application.CommandBars
    

            Select Case objCommandBar.Type
                Case msoBarTypeMenuBar
                    strType = "Menu Bar"
                Case msoBarTypeNormal
                    strType = "Normal"
                Case msoBarTypePopup
                    strType = "Pop-Up"
            End Select
        
            Select Case objCommandBar.Position
                Case msoBarBottom
                    strPosition = "Bottom"
                Case msoBarFloating
                    strPosition = "Floating"
                Case msoBarLeft
                    strPosition = "Left"
                Case msoBarMenuBar
                    strPosition = "Menu Bar"
                Case msoBarPopup
                    strPosition = "Pop-Up"
                Case msoBarRight
                    strPosition = "Right"
                Case msoBarTop
                    strPosition = "Top"
            End Select
        
            Select Case objCommandBar.Protection
                Case msoBarNoChangeDock
                    strProtection = "No Change Dock"
                Case msoBarNoChangeVisible
                    strProtection = "No Change Visible"
                Case msoBarNoCustomize
                    strProtection = "No Customize"
                Case msoBarNoHorizontalDock
                    strProtection = "No Horizontal Dock"
                Case msoBarNoMove
                    strProtection = "No Move"
                Case msoBarNoProtection
                    strProtection = "No Protection"
                Case msoBarNoResize
                    strProtection = "No Resize"
                Case msoBarNoVerticalDock
                    strProtection = "No Vertical Dock"
            End Select
            
            objTextStream.WriteLine objCommandBar.Name & vbTab & _
                strType & vbTab & _
                objCommandBar.Enabled & vbTab & _
                objCommandBar.Visible & vbTab & _
                objCommandBar.Index & vbTab & _
                strPosition & vbTab & _
                strProtection & vbTab & _
                objCommandBar.RowIndex & vbTab & _
                objCommandBar.Top & vbTab & _
                objCommandBar.Height & vbTab & _
                objCommandBar.Left & vbTab & _
                objCommandBar.Width
            
        Next objCommandBar
    
        objTextStream.Close
        
        MsgBox "结果写入 " & objFileSaveDialog.SelectedItems.Item(1) & "."
    
    End If
    
End Sub

Sub CommandBarControlDocumenter()

    ' 用途:将当前应用程序中所有有关命令栏的信息
    ' 写入文本文件。

    ' 您必须先设置对 Microsoft 脚本运行时的引用
    ' 才能使此代码正确运行。
    
    ' 注意:此代码仅适用于 Microsoft Office XP。

    Dim objCommandBar As Office.CommandBar
    Dim objCommandBarControl As Office.CommandBarControl
    Dim strOLEUsage As String
    Dim strType As String
    Dim objFileSaveDialog As Office.FileDialog
    Dim objFSO As Scripting.FileSystemObject
    Dim objTextStream As Scripting.TextStream
    Const SAVE_BUTTON As Integer = -1
    
    Set objFileSaveDialog = Application.FileDialog(msoFileDialogSaveAs)
    
    objFileSaveDialog.Title = "将结果另存为"
    
    ' 用户单击“保存”按钮。
    If objFileSaveDialog.Show = SAVE_BUTTON Then
     
        Set objFSO = New Scripting.FileSystemObject
        Set objTextStream = objFSO.CreateTextFile(objFileSaveDialog.SelectedItems.Item(1))
        
        objTextStream.WriteLine "ID" & vbTab & _
            "Index" & vbTab & _
            "Caption" & vbTab & _
            "Parent" & vbTab & _
            "DescriptionText" & vbTab & _
            "BuiltIn" & vbTab & _
            "Enabled" & vbTab & _
            "IsPriorityDropped" & vbTab & _
            "OLEUsage" & vbTab & _
            "Priority" & vbTab & _
            "Tag" & vbTab & _
            "TooltipText" & vbTab & _
            "Type" & vbTab & _
            "Visible" & vbTab & _
            "Height" & vbTab & _
            "Width"
    
            ' 将下一行替换为:
            ' For Each objCommandBar In Application.ActiveExplorer.CommandBars <- 对于 Outlook
            ' For Each objCommandBar In Application.VBE.CommandBars _
                <- 对于 Visual Basic Editor
            For Each objCommandBar In Application.CommandBars
            
                For Each objCommandBarControl In objCommandBar.Controls
                
                    Select Case objCommandBarControl.OLEUsage
                        

                        Case msoControlOLEUsageBoth
                            strOLEUsage = "Both"
                        Case msoControlOLEUsageClient
                            strOLEUsage = "Client"
                        Case msoControlOLEUsageNeither
                            strOLEUsage = "Neither"
                        Case msoControlOLEUsageServer
                            strOLEUsage = "Server"
                    
                    End Select
                
                    Select Case objCommandBarControl.Type
                    
                        Case msoControlActiveX
                            strType = "ActiveX"
                        Case msoControlAutoCompleteCombo
                            strType = "Auto-Complete Combo Box"
                        Case msoControlButton
                            strType = "Button"
                        Case msoControlButtonDropdown
                            strType = "Drop-Down Button"
                        Case msoControlButtonPopup
                            strType = "Popup Button"
                        Case msoControlComboBox
                            strType = "Combo Box"
                        Case msoControlCustom
                            strType = "Custom"
                        Case msoControlDropdown
                            strType = "Drop-Down"
                        Case msoControlEdit
                            strType = "Edit"
                        Case msoControlExpandingGrid
                            strType = "Expanding Grid"
                        Case msoControlGauge
                            strType = "Gauge"
                        Case msoControlGenericDropdown
                            strType = "Generic Drop-Down"
                        Case msoControlGraphicCombo
                            strType = "Graphic Combo Box"
                        Case msoControlGraphicDropdown
                            strType = "Graphic Drop-Down"
                        Case msoControlGraphicPopup
                            strType = "Popup Graphic"
                        Case msoControlGrid
                            strType = "Grid"
                        Case msoControlLabel
                            strType = "Label"
                        Case msoControlLabelEx
                            strType = "LabelEx"
                        Case msoControlOCXDropdown
                            strType = "OCX Drop-Down"
                        Case msoControlPane
                            strType = "Pane"
                        Case msoControlPopup
                            strType = "Popup"
                        Case msoControlSpinner
                            strType = "Spinner"
                        Case msoControlSplitButtonMRUPopup
                            strType = "Split Button MRU Popup"
                        Case msoControlSplitButtonPopup
                            strType = "Button Popup"
                        Case msoControlSplitDropdown
                            strType = "Split Drop-Down"
                        Case msoControlSplitExpandingGrid
                            strType = "Split Expanding Grid"
                        Case msoControlWorkPane
                            strType = "Work Pane"
            
                        End Select
                            
                        objTextStream.WriteLine objCommandBarControl.ID & _
                          vbTab & _
                            objCommandBarControl.Index & vbTab & _
                            objCommandBarControl.Caption & vbTab & _
                            objCommandBarControl.Parent.Name & vbTab & _
                            objCommandBarControl.DescriptionText & vbTab & _
                            objCommandBarControl.BuiltIn & vbTab & _
                            objCommandBarControl.Enabled & vbTab & _
                            objCommandBarControl.IsPriorityDropped & vbTab & _
                            strOLEUsage & vbTab & _
                            objCommandBarControl.Priority & vbTab & _
                            objCommandBarControl.Tag & vbTab & _
                            objCommandBarControl.TooltipText & vbTab & _
                            strType & vbTab & _
                            objCommandBarControl.Visible & vbTab & _
                            objCommandBarControl.Height & vbTab & _
                            objCommandBarControl.Width
                                                    
                    Next objCommandBarControl
                
                Next objCommandBar
                
                objTextStream.Close
        
                MsgBox "结果写入 " & _
                  objFileSaveDialog.SelectedItems.Item(1) & "."
    
            End If
            
End Sub

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: