明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

對 Microsoft Office 命令欄進行更多編程(3)

[摘要]CommandBarDocumenter 和 CommandBarControlDocumenter 子例程在開發(fā)命令欄解決方案時,我經(jīng)常需要獲取特定命令欄或命令欄控件的索引、名稱或標題。我創(chuàng)建了 CommandBarDocumenter 和 CommandBarControlDocumenter...
CommandBarDocumenter 和 CommandBarControlDocumenter 子例程
在開發(fā)命令欄解決方案時,我經(jīng)常需要獲取特定命令欄或命令欄控件的索引、名稱或標題。我創(chuàng)建了 CommandBarDocumenter 和 CommandBarControlDocumenter 子例程,以便將所有命令欄和命令欄控件的公共屬性記錄在給定的 Office 應用程序中。

要運行這些示例,請在 Visual Basic Editor 中將以下代碼復制到 Microsoft Office XP 應用程序的代碼模塊,然后運行以下子例程之一或兩者都運行。屏幕出現(xiàn)提示時,請將結(jié)果保存為文本文件 (.txt)。這使結(jié)果更容易加載到應用程序(例如 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 = "將結(jié)果另存為"

' 用戶單擊“保存”按鈕。
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 "結(jié)果寫入 " & 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 = "將結(jié)果另存為"

' 用戶單擊“保存”按鈕。
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 "結(jié)果寫入 " & _
objFileSaveDialog.SelectedItems.Item(1) & "."

End If

End Sub