日期:2009-11-03  浏览次数:20466 次

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<