View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Name of Sub/Procedure?

To insert at the cursor line -
Const cPROC As String = "myProcedureName"
with alt-P (button on VBE main menu)

Install the following into your personal.xls or some addin, code in a normal
module and also a class named "cVBEbutton"

' code in normal module in personal.xls or addin
Option Explicit
Private clsBtnEvents As cVBEbutton

' call IDEmenus true/false in open/close events, or manually

Sub auto_openX() ' remove the X
IDEmenu True
End Sub
Sub auto_closeX()
IDEmenu False
End Sub

Sub IDEmenu(bCreate As Boolean)
' add a button on VBE's main menue bar and sub-class its click event
Const cPROC As String = "IDEmenus"

On Error Resume Next
Application.VBE.CommandBars(1).FindControl(Tag:="P rocNameButton").Delete
On Error GoTo errH

Set clsBtnEvents = Nothing

If bCreate Then
Set clsBtnEvents = New cVBEbutton
Set clsBtnEvents.VBbutton = _
Application.VBE.CommandBars(1).Controls.Add(1, temporary:=True)
With clsBtnEvents.VBbutton
.Tag = "ProcNameButton"
.Style = msoButtonCaption
.Caption = "&Proc-Name"
.Parameter = "ProcName"
End With
End If

Exit Sub

errH:
'Debug.Print cPROC & " " & Err.Description
End Sub
' end code in normal module


' code in class "cVBEbutton"
Option Explicit
Public WithEvents VBbutton As CommandBarButton

Private Sub vbButton_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
Const cPROC As String = "vbButton_Click"

Dim nStart As Long ' cursor line
Dim sProcName As String

Select Case Ctrl.Parameter

Case "ProcName"

With Application.VBE.ActiveCodePane
.GetSelection nStart, 0&, 0&, 0&
With .CodeModule
sProcName = Chr(34) & .ProcOfLine(nStart, 0&) & Chr(34)
.InsertLines nStart, "Const cPROC As String = " & sProcName
End With
End With

'Case "some other button tag"

End Select

End Sub
' end code in class cVBEbutton

Obviously adapt to own needs.

Regards,
Peter T

PS - there's no way to trap obtain names in the Stack while code is running.
Can of course break and Ctrl-l


"Charlotte E." wrote in message
...
Is it possible to get the name of the current running Procedure?

Kind of an 'ActiveProcedure.Name' request :-)


Thanks,