Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. Line numbers would be great too if that is possible. Example: sub Main() call Sub end sub sub Sub1() call sub2(3) end sub Sub Sub2(n as long) if n 2 then 'Code to print ' Sub 2 called with value n ' from Sub1 at line 2 ' from Main at line 2 end end if end sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
|
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
On Tue, 13 Jul 2010 14:18:05 -0400, Ron Rosenfeld wrote:
On Tue, 13 Jul 2010 13:43:35 -0400, wrote: If I detect an error in a Sub/Function, I would like to be able to print the sequence of callers. Line numbers would be great too if that is possible. Example: sub Main() call Sub end sub sub Sub1() call sub2(3) end sub Sub Sub2(n as long) if n 2 then 'Code to print ' Sub 2 called with value n ' from Sub1 at line 2 ' from Main at line 2 end end if end sub Check out the UserName property of the Application object. Apparently I am not understanding your reply. Application.UserName returns the name of my computer, not the names in the sub/functuion call chain. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
On Jul 13, 1:43*pm, wrote:
If I detect an error in a Sub/Function, I would like to be able to print the sequence of callers. *Line numbers would be great too if that is possible. Example: sub Main() * * * * call Sub end sub sub Sub1() * * * * call sub2(3) end sub Sub Sub2(n as long) * * if *n 2 then * * * * 'Code to print * * * * ' * *Sub 2 called with value n * * * * ' * from *Sub1 at line 2 * * * * ' * from Main at line 2 * * * * end * * end if end sub You can always "remember" the caller if its name is not exposed: Dim caller As String Sub main() caller = "main" Call sub1 End Sub Sub sub1() MsgBox caller 'do other stuff End Sub In fact, you could even implement a Stack and have each sub push its name onto the stact prior to calling another sub and poping the name after the other sub's return. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
|
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
wrote: On Jul 13, 1:43*pm, wrote: If I detect an error in a Sub/Function, I would like to be able to print the sequence of callers. *Line numbers would be great too if that is possible. Example: sub Main() * * * * call Sub end sub sub Sub1() * * * * call sub2(3) end sub Sub Sub2(n as long) * * if *n 2 then * * * * 'Code to print * * * * ' * *Sub 2 called with value n * * * * ' * from *Sub1 at line 2 * * * * ' * from Main at line 2 * * * * end * * end if end sub You can always "remember" the caller if its name is not exposed: Dim caller As String Sub main() caller = "main" Call sub1 End Sub Sub sub1() MsgBox caller 'do other stuff End Sub In fact, you could even implement a Stack and have each sub push its name onto the stact prior to calling another sub and poping the name after the other sub's return. Thanks for your reply. I was hoping to use the capability to help generate a message when a routine somewhere down a call chain detected an unexpected error. It would be used with already written routines. I know that VBA must already maintain a call stack. I was hoping that, in it might contain caller names & perhaps line numbers and that I could access info in the stack. It sounds like there is no such capability. You are, of course, correct that I could maintain my own stack but that would require code before and after each call which would be difficult for subs and much more clumsy when using functions. i = f(3 + g(4)) + h(3.14159, SomethingElse) I was hoping that being an intrepreted rather than compiled language (at least to a degree) VBA might include such a capability. Thanks anyway. |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can a Sub/Function print the names of its callers?
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
wrote: On Jul 13, 1:43*pm, wrote: If I detect an error in a Sub/Function, I would like to be able to print the sequence of callers. *Line numbers would be great too if that is possible. Example: sub Main() * * * * call Sub end sub sub Sub1() * * * * call sub2(3) end sub Sub Sub2(n as long) * * if *n 2 then * * * * 'Code to print * * * * ' * *Sub 2 called with value n * * * * ' * from *Sub1 at line 2 * * * * ' * from Main at line 2 * * * * end * * end if end sub You can always "remember" the caller if its name is not exposed: Dim caller As String Sub main() caller = "main" Call sub1 End Sub Sub sub1() MsgBox caller 'do other stuff End Sub In fact, you could even implement a Stack and have each sub push its name onto the stact prior to calling another sub and poping the name after the other sub's return. Thanks for your reply. I was hoping to use the capability to help generate a message when a routine somewhere down a call chain detected an unexpected error. It would be used with already written routines. I know that VBA must already maintain a call stack. I was hoping that, in it might contain caller names & perhaps line numbers and that I could access info in the stack. It sounds like there is no such capability. You are, of course, correct that I could maintain my own stack but that would require code before and after each call which would be difficult for subs and much more clumsy when using functions. i = f(3 + g(4)) + h(3.14159, SomethingElse) I was hoping that being an intrepreted rather than compiled language (at least to a degree) VBA might include such a capability. Thanks anyway. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Print list of ws names | New Users to Excel | |||
How to print out matching names? | Excel Programming | |||
Print to PDF with SHEET names | Excel Discussion (Misc queries) | |||
Print range names and how know if have too many? | Excel Worksheet Functions |