Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the procedure or function name at runtime?

Tags:

People also ask

What is a procedure name in VBA?

VBA Naming Conventions Procedure NamesName them after what they're doing, using a verb. If accurately naming a procedure is not possible, likely the procedure is doing too many things and needs to be broken down into smaller, more specialized procedures.

How do you call a procedure or a function in VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses.


Is there any way to return the name of a function or procedure at runtime?

I'm currently error handling something like this:

Sub foo() Const proc_name as string = "foo" On Error GoTo ErrHandler      ' do stuff  ExitSub:     Exit Sub ErrHandler:     ErrModule.ShowMessageBox "ModuleName",proc_name     Resume ExitSub End Sub 

I recently experienced one of my constants lying to me after I updated a function name, but not the constant value. I want to return the name of the procedure to my error handler.

I know that I will have to interact with the VBIDE.CodeModule object to find it. I've done a little bit of meta-programming with the Microsoft Visual Basic for Applications Extensibility library, but I've not had any success with doing this at runtime. I don't have my previous attempts, and before I dig my heels in to try this again, I want to know if it's even remotely possible.

Things that won't work

  1. Using some built in VBA Library to access the call stack. It doesn't exist.
  2. Implementing my own call stack by pushing and popping procedure names from an array as I enter and exit each one. This still requires that I pass the proc name somewhere else as a string.
  3. A third party tool like vbWatchDog. This does work, but I can't use a third party tool for this project.

Note

vbWatchdog seems to do this by directly accessing the kernel memory via API calls.