對 Microsoft Office 命令欄進行更多編程 Frank C. Rice Microsoft Corporation Paul Cornell Microsoft Corporation 2002 年 5 月 2 日
在上個月的專欄(英文)中,我向您介紹了“命令欄”,它們是在 Microsoft® Office 中使用的用戶界面組件,使用戶能夠在 Office 應用程序中執行操作。根據您對上個月專欄的反饋,我將向您介紹一些用于解決命令欄特定問題的附加信息和代碼,具體內容包括:
Microsoft Outlook® 如何以編程方式處理命令欄。 如何向命令欄按鈕中添加自定義圖像。 如何向命令欄中添加組合框。 如何禁用和隱藏命令欄以及命令欄控件。 如何定位命令欄。 如何動態添加和刪除命令欄。 如何在給定的 Office 應用程序中列出命令欄和命令欄控件的通用屬性。 命令欄和 Outlook 對象模型 在上個月的專欄中,我忘了告訴您 Microsoft Outlook 對象模型訪問命令欄和命令欄控件的方式與其他 Microsoft Office 應用程序略有不同。
在除 Outlook 以外的應用程序中,您可以使用如下所示的代碼訪問命令欄:
Public Sub ListCommandBarNames()
' 用途:列出當前應用程序的所有命令欄名稱。 ' 注意:此代碼對 Outlook 無效!
Dim objCommandBar As Office.CommandBar
For Each objCommandBar In Application.CommandBars
Debug.Print objCommandBar.Name
Next objCommandBar
End Sub
然而,嘗試在 Outlook 中運行此代碼將導致運行時錯誤。相反,您必須使用 Explorer 或 Inspector 對象的 CommandBars 屬性,如下所示:
Public Sub ListOutlookExplorerCommandBarNames()
' 用途:列出當前資源管理器的所有命令欄名稱。 ' 注意:此代碼只對 Outlook 有效!
Dim objCommandBar As Office.CommandBar
For Each objCommandBar In Application.ActiveExplorer.CommandBars
Debug.Print objCommandBar.Name
Next objCommandBar
End Sub
在前面的代碼示例中,將代碼 ActiveExplorer 替換為 ActiveInspector 可打印活動檢查器的所有命令欄名稱。對于那些不熟悉 Outlook 對象模型的用戶,“瀏覽器”表示 Outlook 用戶界面。“檢查器”表示一個窗口,它包含特定的 Outlook 項(如電子郵件信息或聯系人)以及 Outlook 項中的任何選項卡頁(如任務項中的“詳細信息”選項卡)。
向命令欄按鈕中添加自定義圖像 盡管可以使用 CommandBarButton 對象的 FaceId 屬性將命令欄按鈕的圖像設置為 Office 提供的內置圖像,但您可以使用 CommandBarButton 對象的 Picture 屬性提供您創建的圖像,也可以使用 CommandBarButton 對象的 Mask 屬性創建自定義透明圖像。
盡管在 Web 的共享軟件和免費軟件站點上有很多可用的圖像編輯器,以及 Microsoft Visual C++® 這樣的工具,但是要創建這些圖像,使用 Microsoft 畫圖就足夠了。要使用畫圖創建這些圖像:
在“開始”菜單上,指向“程序”,指向“附件”,然后單擊“畫圖”。 在“圖像”菜單上,單擊“屬性”。 在“寬度”框中,鍵入“16”。在“高度”框中,鍵入“16”。確保選中“像素”和“彩色”選項,然后單擊“確定”。 在“查看”菜單上,指向“縮放”,然后單擊“自定義”。 單擊“800%”選項,然后單擊“確定”。 在“查看”菜單上,指向“縮放”,然后單擊“顯示網格”。 在“查看”菜單上,確保選中“工具箱”和“顏料盒”命令。 使用“工具箱”和“顏料盒”控件創建圖像。 創建完圖像之后,在“文件”菜單上,單擊“保存”。 將圖標保存為“256 色位圖”。 下面是我創建的圖像示例:
圖 1:自定義的不透明位圖
要創建透明圖像,您必須創建相應的“圖像掩碼”。為此,請保存剛剛創建的圖像,但要更改文件名。對于每個需要透明的像素,請用白色填充該像素。對于每個需要不透明的像素,請用黑色填充該像素。然后再次保存該圖像。下面是我創建的圖像掩碼示例:
圖 2:自定義位圖掩碼
下面是一些示例代碼,顯示如何向命令欄按鈕中添加透明圖片:
Public Sub NewPictureOnNewCommandBar()
' 用途:向命令欄按鈕中添加圖片。
Dim objCommandBar As Office.CommandBar Dim objCommandBarButton As Office.CommandBarButton Dim objPicture As stdole.IPictureDisp
' 如果不需要透明圖像,請將下一行代碼注釋掉。 Dim objMask As stdole.IPictureDisp
Const PICTURE_PATH As String = "C:\My Pictures\OK.bmp"
' 如果不需要透明圖像,請將下一行代碼注釋掉。 Const PICTURE_MASK As String = "C:\My Pictures\OKMask.bmp"
Const COMMAND_BAR_NAME As String = "測試命令欄"
' 將下一行替換為: ' For Each objCommandBar In Application.ActiveExplorer.CommandBars <- 對于 Outlook ' For Each objCommandBar In Application.VBE.CommandBars <- 對于 Visual Basic Editor For Each objCommandBar In Application.CommandBars
If objCommandBar.Name = COMMAND_BAR_NAME Then
objCommandBar.Delete
End If
Next objCommandBar
Set objCommandBar = Application.CommandBars.Add(COMMAND_BAR_NAME) Set objCommandBarButton = _ objCommandBar.Controls.Add(msoControlButton) Set objPicture = LoadPicture(PICTURE_PATH)
' 如果不需要透明圖像,請將下一行代碼注釋掉。 Set objMask = LoadPicture(PICTURE_MASK)
objCommandBarButton.Picture = objPicture
' 如果不需要透明圖像,請將下一行代碼注釋掉。 objCommandBarButton.Mask = objMask
End Sub
如前面的代碼所示,請在代碼中為不透明圖像創建一個對象,為圖像掩碼創建一個對象;每個對象都必須為 stdole.IPictureDisp 類型。接下來,通過調用對象的 LoadPicture 方法對這兩個對象分別進行初始化。最后,將命令欄按鈕對象的 Picture 屬性設置為不透明圖像對象,將命令欄按鈕對象的 Mask 屬性設置為圖像掩碼。
以下是不透明和透明圖像的最終外觀:
圖 3:命令欄按鈕上的不透明和透明圖像
順便說一下,請勿將 CommandBarButton 對象的 FaceId 屬性與 CommandBarButton 對象的 Id 屬性混淆在一起。Id 屬性確定該命令欄控件的內置操作。所有自定義命令欄控件 Id 屬性的默認值均為 1。將自定義命令欄控件的 Id 屬性設置為 1 以外的數字會將自定義命令欄控件的操作設置為內置操作(前提是應用程序中存在此 ID 的內置操作)。為了便于參考,下列代碼列出了應用程序中所有命令欄控件的所有 ID:
Public Sub ListCommandBarControlIDs()
' 用途:列出當前應用程序所有命令欄控件的 ID。
Dim objCommandBar As Office.CommandBar Dim objCommandBarControl As Office.CommandBarControl
' 將下一行替換為: ' 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
Debug.Print objCommandBarControl.Caption & " " & _ objCommandBarControl.ID
Next objCommandBarControl
Next objCommandBar
End Sub
向命令欄中添加組合框 要向命令欄中添加組合框,請使用 CommandBarControls 集合的 Add 方法,并將 msoControlComboBox 枚舉常數傳遞給 Type 參數。然后使用 CommandBarComboBox 對象的 AddItem 方法向組合框中添加選項。
以下函數向現有命令欄中添加組合框:
Public Function AddComboBoxToCommandBar(ByVal strCommandBarName As String, _ ByVal strComboBoxCaption As String, _ ByRef strChoices() As String) As Boolean
' 用途:向命令欄中添加組合框。 ' 接受: ' strCommandBarName:添加組合框的命令欄名稱。 ' strChoices():組合框選項數組。 ' 返回:如果組合框已成功添加至命令欄中,則為 True。
Dim objCommandBarControl As Office.CommandBarControl Dim objCommandBarComboBox As Office.CommandBarComboBox Dim varChoice As Variant
On Error GoTo AddComboBoxToCommandBar_Err
' 刪除以前添加的此組合框的所有實例。 ' 將下一行代碼替換為: ' For Each objCommandBarControl In _ ' Application.ActiveExplorer.CommandBars.Item(strCommandBarName).Controls _ <- 對于 Outlook ' For Each objCommandBarControl In _ ' Application.VBE.CommandBars.Item(strCommandBarName).Controls _ <- 對于 Visual Basic Editor For Each objCommandBarControl In Application.CommandBars.Item(strCommandBarName).Controls
If objCommandBarControl.Caption = strComboBoxCaption Then
objCommandBarControl.Delete
End If
Next objCommandBarControl
' 創建組合框。 ' 將下一行代碼替換為: ' Set objCommandBarComboBox = _ ' Application.CommandBars.Item(strCommandBarName).Controls.Add(msoControlComboBox) _ <- 對于 Outlook ' Set objCommandBarComboBox = _ ' Application.CommandBars.Item(strCommandBarName).Controls.Add(msoControlComboBox) _ <- 對于 Visual Basic Editor Set objCommandBarComboBox = _ Application.CommandBars.Item(strCommandBarName).Controls.Add(msoControlComboBox) objCommandBarComboBox.Caption = strComboBoxCaption
For Each varChoice In strChoices
objCommandBarComboBox.AddItem varChoice
Next varChoice
AddComboBoxToCommandBar_End:
AddComboBoxToCommandBar = True Exit Function
AddComboBoxToCommandBar_Err:
AddComboBoxToCommandBar = False
End Function
|