VBA Microsoft Visual Basic for Applications Extensibility 5.3 Library

The VBIDE library can be used to interact with VBA Projects and the Visual Basic Editor.

Classes

Class Description
AddIn Represents an AddIn.
AddIns Collection of AddIn objects accessed through the VBE object.
CodeModule Represents a code module.
CodePane Represents a code pane.
CodePanes Collection of CodePane objects accessed through the VBE object.
CommandBarEvents Triggers an event when a control on the command bar is clicked.
Events Supplies properties that enable add-ins to connect to all events in VBA.
LinkedWindows Collection containing all linked windows in a linked window frame.
Properties Represents the properties of an object. Use the Properties collection to access the properties displayed in the Properties window. For every property listed in the Properties window, there is a Property object in the Properties collection.
Property Represents a Property of an object that is visible in the Properties window.
Reference Represents a reference to a type library or a project.
References Represents the set of references in the project. Use the References collection to add or remove references. The References collection is the same as the set of references selected in the References dialog box.
ReferencesEvents The source of events that occur when a reference is added to or removed from a VBA project.
VBComponent Represents a component contained in a VBA project, such as a class module or standard module.
VBComponents Represents the components in a VBA project.
VBE Represents the Visual Basic Editor.
VBProject Represents a VBA project.
VBProjects Represents all open VBA projects available in the development environment.
Window Represents a window in the development environment.
Windows Collection of all open or permanent windows.

Enums and Consts

vbext_CodePaneview
  1. vbext_cv_FullModuleView = 1
  2. vbext_cv_ProcedureView = 0
vbext_ComponentType
  1. vbext_ct_ActiveXDesigner = 11
  2. vbext_ct_ClassModule = 2
  3. vbext_ct_Document = 100
  4. vbext_ct_MSForm = 3
  5. vbext_ct_StdModule = 1
vbext_ProcKind
  1. vbext_pk_Get = 3
  2. vbext_pk_Let = 1
  3. vbext_pk_Proc = 0
  4. vbext_pk_Set = 2
vbext_ProjectProtection
  1. vbext_pp_locked = 1
  2. vbext_pp_none = 0
vbext_ProjectType
  1. vbext_pt_HostProject = 100
  2. vbext_pt_StandAlone = 101
vbext_RefKind
  1. vbext_rk_Project = 1
  2. vbext_rk_TypeLib = 0
vbext_VBAMode
  1. vbext_vm_Break = 1
  2. vbext_vm_Design = 2
  3. vbext_vm_Run = 0
vbext_WindowState
  1. vbext_ws_Maximize = 2
  2. vbext_ws_Minimize = 1
  3. vbext_ws_Normal = 0
vbext_WindowType
  1. vbext_wt_Browser = 2
  2. vbext_wt_CodeWindow = 0
  3. vbext_wt_Designer = 1
  4. vbext_wt_Find = 8
  5. vbext_wt_FindReplace = 9
  6. vbext_wt_Immediate = 5
  7. vbext_wt_LinkedWindowFrame = 11
  8. vbext_wt_Locals = 4
  9. vbext_wt_MainWindow = 12
  10. vbext_wt_ProjectWindow = 6
  11. vbext_wt_PropertyWindow = 7
  12. vbext_wt_Toolbox = 10
  13. vbext_wt_ToolWindow = 15
  14. vbext_wt_Watch = 3

Use Cases

Use cases of the VBIDE library include working with project references and inserting code in a code module.

Work with References

The VBIDE library can be used to add, remove, and get information about library references.

Public Sub SetProjectReferenceByFilePath(FilePath As String)

    'Sets Project Reference

    Dim VBProj    As Object 'VBIDE.VBProject
    Dim Refs      As Object 'VBIDE.References
    Dim Ref       As Object 'VBIDE.Reference
    Dim RefFound  As Boolean

    Set VBProj = ThisWorkbook.VBProject
    Set Refs = VBProj.References

    For Each Ref In Refs
        If Ref.FullPath = FilePath Then
            RefFound = True
            Exit For
        End If
    Next Ref

    If Not RefFound Then
        VBProj.References.AddFromFile FilePath
    End If

End Sub
Public Sub PrintReferences()

    Dim Proj As Object 'VBIDE.VBProject
    Set Proj = ThisWorkbook.VBProject

    Dim Refs As Object 'VBIDE.References
    Set Refs = Proj.References

    Dim Ref As Object 'VBIDE.Reference
    For Each Ref In Refs
        With Ref
            Debug.Print "***********************************************"
            Debug.Print "Name: " & .Name
            Debug.Print "Major: " & .Major
            Debug.Print "Minor: " & .Minor
            Debug.Print "Description: " & .Description
            Debug.Print "FullPath: " & .FullPath
            Debug.Print "GUID: " & .GUID
            Debug.Print "BuiltIn: " & .BuiltIn
            Debug.Print "IsBroken: " & .IsBroken
            Debug.Print "Type: " & .Type
            Debug.Print "***********************************************"
        End With
    Next Ref

End Sub

Add Code to a Module

The VBIDE library can be used to add code to a code module.

Public Sub Example()

    Dim Proj As VBIDE.VBProject
    Set Proj = ThisWorkbook.VBProject

    Dim C As VBIDE.VBComponent
    Set C = Proj.VBComponents("Module1")

    Dim CM As VBIDE.CodeModule
    Set CM = C.CodeModule

    CM.InsertLines _
        CM.CountOfLines + 1, _
        vbNewLine & _
        "Public Sub Example()" & _
        String$(2, vbNewLine) & _
        vbTab & "Debug.Print ""Hello, World!""" & _
        String$(2, vbNewLine) & _
        "End Sub"

End Sub