Advanced VBE (Visual Basic Editor) Automation code example

Advanced VBE (Visual Basic Editor) Automation code example

VBE (Visual Basic Editor)

Microsoft offers a power of real scripting language which can work over existing scripts and its architecture like getting list of available components. Followings are the list of components available in VBE environment:

  • Projects
  • Standard Modules
  • Class Modules
  • Procedures
  • Functions
  • User Forms and many more.

VBIDE

Lets have a look at VBE automation. the very first thing that developer need to add a reference in VBA project to access VBIDE class as shown below:

Note: VBE automation always blocked by System because of Project Trust as developer is trying to access program within program. Hence, you must have to Trust your project in your application by following steps:

  1. File Menu
  2. Options
  3. Trust Center>>Trust Center Settings…
  4. Macro Settings
  5. Enable/Check Trust access to the VBA project object model under Developer macro settings

Code example

Function ListAllMacros() As String

	Dim proj As VBProject
	Dim vbcomp As VBComponent
	Dim curMacro As String
	Dim newMacro As String
	Dim x As String
	Dim y As String
	Dim macros As String

	curMacro = ""

	For Each proj In Application.VBE.VBProjects
		For Each vbcomp In proj.VBComponents
			   If Not vbcomp Is Nothing Then
				   If vbcomp.CodeModule = "modLogic" Then
					   For i = 1 To vbcomp.CodeModule.CountOfLines
						  newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, _
							 prockind:=vbext_pk_Proc)

						  If curMacro <> newMacro Then
							 curMacro = newMacro

							   If curMacro <> "" And curMacro <> "Sort_DataMacro" Then
								   macros = curMacro + " " + macros
							   End If

						  End If
					   Next
				   End If
			   End If
		Next vbcomp
	Next proj

	ListAllMacros = macros

End Function

Next>> ActiveX controls VBA

Leave a Reply

Your email address will not be published.