Breakpoint Line Numbers in VB Editor


Breakpoint Line Numbers in VB Editor



Does anyone know how to get line numbers of breakpoints in the VB Editor programmatically? I can't find a way to do this currently with the VBA Extensibility Library (Microsoft Visual Basic for Applications Extensibility 5.3).



FYI, I've already numbered my lines (starting with 10 and increasing by 10 for each line, excluding Dim's)


Dim





Can you confirm you're talking about "Visual Basic for Applications" - (e.g. the VBA editor in Microsoft Office?), if so, which host-application are you using?
– Dai
Jun 30 at 19:47





Yes, Visual Basic for Applications (VBA). I'm using Excel 2016. I'd like to pull the line numbers where programmer has put breakpoints (not Stop statements in the code, but specifically breakpoints signaled by the red circles)
– A. Hendry
Jun 30 at 20:00


Stop





I am pretty sure that you will need to create an Excel VSTO Add-in project in Visual Studio. MZ Tools Trail cost $64, but it has a lot f useful features. Including line numbering . VBA Rubberduck is also feature packed for the low low price of free.
– TinMan
Jun 30 at 20:53





Why would you need to the breakpoint numbers?
– TinMan
Jun 30 at 20:54





Just for logging different testing for personal reasons. Ultimately, I'd like to program the Margin Indicator Bar if I could. I have Rubberduck and MZ Tools, but they do not provide this functionality. I'm wondering if I can program the Margin Indicator Bar in VBA or if I have to write a COM Add-In in another language.
– A. Hendry
Jun 30 at 20:59


Rubberduck


MZ Tools




1 Answer
1



I know of no easy way to get the line number of Breakpoints in the VBEditor.
But you can get the line number of the cursor in the ActiveCodePane using the Microsoft Visual Basic for Applications Extensibility 5.3 library. With this information you can run a function from the Immediate Window to log all your data.


ActiveCodePane


Immediate Window



These are the main method of the ActiveCodePane and CodeModule that can be used to extract the information.


ActiveCodePane


CodeModule


Sub GetSelection(StartLine As Long, StartColumn As Long, EndLine As Long, EndColumn As Long)

Property ProcOfLine(Line As Long, ProcKind As vbext_ProcKind) As String



enter image description here
Sub LogBreakPoint()
Dim StartLine As Long, StartColumn As Long, EndLine As Long, EndColumn As Long
Dim ProcName As String, Text As String


With Application.VBE.ActiveCodePane
.GetSelection StartLine, StartColumn, EndLine, EndColumn
With .CodeModule
ProcName = .ProcOfLine(Line:=StartLine, ProcKind:=vbext_pk_Get)
Text = .Lines(StartLine, 1)
End With
End With

With Worksheets("BreakPoint Log")
With .Range("A" & .Rows.count).End(xlUp).Offset(1)
Debug.Print Join(Array(StartLine, StartColumn, EndLine, EndColumn, ProcName, Text), ",")
.Resize(1, 6).Value = Array(StartLine, StartColumn, EndLine, EndColumn, ProcName, Text)
End With
End With
End Sub



Here is a good overview of the VBE Chip Pearson - Programming The VBA Editor.



If you really want to get the line numbers of the Breakpoints you call use SetWindowsHookEx to hook the F9 key. If you want to know if the ActiveCodePane.Window was clicked, you will need to hook the left mouse button, find out what window is under the mouse, calculate the mouse position over the window. Fun Stuff. The main thing that you need to know about WinApi Hooks is save often.


SetWindowsHookEx


ActiveCodePane.Window


WinApi Hooks


Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (_
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long





Thank you, that is very good information. FWIW, I also found something on how to create add-ins for the VBA Editor: <youtube.com/watch?v=y81Aq4bebZU&gt;.
– A. Hendry
Jul 1 at 3:11






Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports Extensibility <ComVisible(True), Guid("C3BC0970-0D67-4518-86C3-E3D693E55287"), ProgId("Test.Connect")> Public Class Connect Implements Extensibility.IDTExtensibility2 Private Sub OnConnection(Application As Object, ConnectMode As Extensibility.ext_ConnectMode, AddInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection Select Case ConnectMode Case Extensibility.ext_ConnectMode.ext_cm_Startup 'Do Stuff Case Extensibility.ext_ConnectMode.ext_cm_AfterStartup
– A. Hendry
Jul 1 at 3:19


Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports Extensibility <ComVisible(True), Guid("C3BC0970-0D67-4518-86C3-E3D693E55287"), ProgId("Test.Connect")> Public Class Connect Implements Extensibility.IDTExtensibility2 Private Sub OnConnection(Application As Object, ConnectMode As Extensibility.ext_ConnectMode, AddInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection Select Case ConnectMode Case Extensibility.ext_ConnectMode.ext_cm_Startup 'Do Stuff Case Extensibility.ext_ConnectMode.ext_cm_AfterStartup






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters