VBE CreateToolWindow

原文:http://www.codeproject.com/Articles/310710/VBE-CreateToolWindow?msg=4857655#xx4857655xx

在 VBA 编辑器中创建可停靠窗口。

介绍

在 VBA 编辑器中创建可停靠窗口时,你必需确保你创建的是 VBE(VBA) 外接程序,而不是 Excel 外接程序。此文章向你展示了如何使用 CreateToolWindow 方法在VBA 编辑器中创建一个新的自定义窗口。

背景

网络上很多关于 Office 外接程序的文章,都是讨论 Office COM 外接程序。但很少有关于 Visual Basic Editor 外接程序的讨论。但 VBE 同样也是可以定制的,它可以创建 工具栏、菜单、停靠窗口。相关的API文档,请查看 http://msdn.microsoft.com/en-us/library/aa443985(v=vs.60).aspx.

CreateToolWindow 方法只能用于 VBE COM 外接程序,而不能用于 Excel 外接程序。CreateToolWindow 方法创建一个新的工具窗口,此窗口被包含在一个指定的 UserDocument 对象中。

使用代码

你可以为 从 OnConnection 事件中调用 Run 方法使构建一个 VBE COM 外接程序。它与 Excel 外接程序 唯一的不同在于 VBE 外接程序的注册表项需要要放置键:HKEY_CURRENT_USER/Software/Microsoft/VBA/VBE/6.0/Addins.

VB6 项目

在 VB6 中创建一个新的 ActiveX DLL 项目,命令为 VBEDemo 同时引用以下组件:

  • Microsoft Add-In Designer
  • Microsoft Visual Basic for Applications Extensibility 5.3
  • Microsoft Excel 14.0 Object Library
  • Microsoft Office 14.0 Object Library

添加一个命名为 VBEConnect 的 Class 到项目,并实现IDTExtensibilty2 接口。添加 User Document 到项目,命名为 CoolDoc,这个 User Document 将关联在 VBE 新的工具窗口中。在顶层类型中声明2个私有变量,m_colldoc As CoolDocm_window as VBIDE.Window. m_window 对象用于保存我们即将创建的 Tool 窗口。现在 VBEConnect 类看起来,应该像下面这样:

Option Explicit

Implements AddInDesignerObjects.IDTExtensibility2

Private m_cooldoc As CoolDoc
private m_window as VBIDE.Window

Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'do nothing
End Sub

Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
'do nothing
End Sub

Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
        ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
        ByVal AddInInst As Object, custom() As Variant)

On Error GoTo errorHandler

Dim app As VBIDE.VBE
Set app = Application

Set m_window = app.Windows.CreateToolWindow(AddInInst, "VBEDemo.CoolDoc", _
                   "My CoolDoc", "anystring",  m_cooldoc)
m_window.Visible = True

errorHandler:
Select Case Err.Number
Case 0
Case Else
    Debug.Print Err.Number & "  " & Err.Description
End Select
End Sub

Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As _
            AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
'do nothing
End Sub

Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'do nothing
End Sub

奇迹发生在 OnConnection 事件,这儿声明了一个 VBIDE.VBE 应用程序对象,它用于调用 CreateToolWindow 方法。此方法使用当前的 外接程序 和 User Document 作为参数。AddInInst 必需是 VBE 外接程序,而不能是 Excel 外接程序。

在上一个项目中,我让使用 Run 方法 和 执行 CreateToolWindow,初始化一个 VBE 外接程序,但此时 外接程序还不能执行任何操作。

Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
        ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
        ByVal AddInInst As Object, custom() As Variant)

Dim gxlApp as Excel.Application
Set gxlApp = Application

Dim gxCAI as Excel.Addin
Set gxCAI = AddInInst

On Error Resume Next
''' Force the VBE to initialize.
gxlApp.Run "xhYe1lsGtd3soe2t4ns"
On Error GoTo errorHandler

Set m_window = app.Windows.CreateToolWindow(AddInInst, "VBEDemo.CoolDoc", 
                   "My CoolDoc", "anystring",  m_cooldoc)
m_window.Visible = True

errorHandler:
Select Case Err.Number
Case 0
Case Else
    Debug.Print Err.Number & " " & Err.Description
End Select
End Sub

运行代码

Before the add-in will work, it is necessary to compile the DLL and add it to the Registry using the regsvr32.exe command. The proper keys need to be created in the Windows Registry in the following location: HKEY_CURRENT_USER/Software/Microsoft/VBA/VBE/6.0/Addins.

The new key must be named after the project name and the class that is implementing the IDTExtensibility2 interface: VBEDemo.VBEConnect. The key needs to contain two DWORDs and two string values:

DWORD: CommandLineSafe | 0
DWORD: LoadBehavior | 3
String value: FriendlyName | VBEDemoToolWindow
String value: Description | A Tool Window VBE Addin Demo
When the Excel VBA editor is started, the add-in will load and the Tool window will appear. Of course, this does not do anything since no functionality has been added, but I had a grin from ear to ear having spent some hours searching the web to find out how to do it (without success if I may add) and reverting to trial and error. I hope this little super simple article may save other people a couple of hours.

History
First version: 2012-01-05 (yes, some people still use VB6).
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

posted @ 2016-09-29 10:09  小BIBO  阅读(672)  评论(0编辑  收藏  举报