Retrieve name of current sub/function and name of current module 
Author Message
 Retrieve name of current sub/function and name of current module

Hi all,

I would like to write some code to retrieve the name of the current module
and the name of the current sub or function. For example, if I have a module
called modTest and a function called blnTest, then I would like to get those
names.

Function blnTest as boolean()
  Debug.Print <name of current module>
  Debug.Print <name of current sub/function>
End Function

I really don't know where to look... I tried
Workbooks("test.xls").VBProject.Name, but that gives me only the name of the
project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name, but
that gives me the name of the CodeModule that is currently on top of the
vb-editor. And that is not allways the module containing the current sub or
function!

Does anybody have a clue? thanx in advance!!!

Marga.

http://www.*-*-*.com/ ~in004323



Wed, 26 Jun 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
I can give you one clue. Each in a VB project becomes part of the
VBProject collection.

You can get the name of each module in your VBProject by looping through
the components and getting the Name property for each component.Something
like this:

Sub printman()

For i = 1 To VBProject.VBComponents.Count

MsgBox VBProject.VBComponents(i).Name

Next i

End Sub

Now, I have no idea how you can use this to get the name of the currently
active module or the currently running sub/function, but I think this
would be better than using the VBE object.

Quote:

> Hi all,

> I would like to write some code to retrieve the name of the current module
> and the name of the current sub or function. For example, if I have a module
> called modTest and a function called blnTest, then I would like to get those
> names.

> Function blnTest as boolean()
>   Debug.Print <name of current module>
>   Debug.Print <name of current sub/function>
> End Function

> I really don't know where to look... I tried
> Workbooks("test.xls").VBProject.Name, but that gives me only the name of the
> project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name, but
> that gives me the name of the CodeModule that is currently on top of the
> vb-editor. And that is not allways the module containing the current sub or
> function!

> Does anybody have a clue? thanx in advance!!!

> Marga.

> http://www.euronet.nl/~in004323



Sat, 29 Jun 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
First a simple rule in VB: .count for collections always start at 0 so the
line should have read
For i = 0 To VBProject.VBComponents.Count-1
Secondly as a possible solution couldn't you get each sub and function to
use the registry or similar to say what it is when it starts? Given that you
are using Excel a routine shouldn't be that difficult to fathom out to
automatically add a few lines of code to each sub and function that doesn't
already have them. Also if I remember correctly there is a Calls box in
Excel which could do what you ask if this is for debugging purposes.

Hope this helps

BTBB



Quote:
> I can give you one clue. Each in a VB project becomes part of the
> VBProject collection.

> You can get the name of each module in your VBProject by looping through
> the components and getting the Name property for each component.Something
> like this:

> Sub printman()

> For i = 1 To VBProject.VBComponents.Count

> MsgBox VBProject.VBComponents(i).Name

> Next i

> End Sub

> Now, I have no idea how you can use this to get the name of the currently
> active module or the currently running sub/function, but I think this
> would be better than using the VBE object.


> > Hi all,

> > I would like to write some code to retrieve the name of the current
module
> > and the name of the current sub or function. For example, if I have a
module
> > called modTest and a function called blnTest, then I would like to get
those
> > names.

> > Function blnTest as boolean()
> >   Debug.Print <name of current module>
> >   Debug.Print <name of current sub/function>
> > End Function

> > I really don't know where to look... I tried
> > Workbooks("test.xls").VBProject.Name, but that gives me only the name of
the
> > project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name,
but
> > that gives me the name of the CodeModule that is currently on top of the
> > vb-editor. And that is not allways the module containing the current sub
or
> > function!

> > Does anybody have a clue? thanx in advance!!!

> > Marga.

> > http://www.euronet.nl/~in004323



Sun, 30 Jun 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
Just two {*filter*}s on the BTBB's reply below:

First: We're doing Word here not Excel or atleast that's what I thought
since I caught the post in the word97vba newsgroup.

Second: Yep, I know that normally things in a collection start with 0,
however, not always and not in this case. The first thing I tried was the
following simple line of code:

      MsgBox VBProject.VBComponents(0).Name

and I got back the following error message:

     Runtime Error 9: Subscript out of Range

and sometimes:

    Runtime Error 5941: The requested member of the collection does not
exist.

I also built a loop exactly as is illustrated by BTBB below and got the same
error until I fixed it to the way it was in my original post.

Another couple of instances I can specifically think of when a collection
does not start at 0 are:

    * Paragraphs
    * BuiltInDocumentProperties
    * Documents
    * Sentences

But the idea about the Registry is very sound.


Quote:
> First a simple rule in VB: .count for collections always start at 0 so the
> line should have read
> For i = 0 To VBProject.VBComponents.Count-1
> Secondly as a possible solution couldn't you get each sub and function to
> use the registry or similar to say what it is when it starts? Given that
you
> are using Excel a routine shouldn't be that difficult to fathom out to
> automatically add a few lines of code to each sub and function that
doesn't
> already have them. Also if I remember correctly there is a Calls box in
> Excel which could do what you ask if this is for debugging purposes.

> Hope this helps

> BTBB



> > I can give you one clue. Each in a VB project becomes part of the
> > VBProject collection.

> > You can get the name of each module in your VBProject by looping through
> > the components and getting the Name property for each
component.Something
> > like this:

> > Sub printman()

> > For i = 1 To VBProject.VBComponents.Count

> > MsgBox VBProject.VBComponents(i).Name

> > Next i

> > End Sub

> > Now, I have no idea how you can use this to get the name of the
currently
> > active module or the currently running sub/function, but I think this
> > would be better than using the VBE object.


> > > Hi all,

> > > I would like to write some code to retrieve the name of the current
> module
> > > and the name of the current sub or function. For example, if I have a
> module
> > > called modTest and a function called blnTest, then I would like to get
> those
> > > names.

> > > Function blnTest as boolean()
> > >   Debug.Print <name of current module>
> > >   Debug.Print <name of current sub/function>
> > > End Function

> > > I really don't know where to look... I tried
> > > Workbooks("test.xls").VBProject.Name, but that gives me only the name
of
> the
> > > project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name,
> but
> > > that gives me the name of the CodeModule that is currently on top of
the
> > > vb-editor. And that is not allways the module containing the current
sub
> or
> > > function!

> > > Does anybody have a clue? thanx in advance!!!

> > > Marga.

> > > http://www.*-*-*.com/ ~in004323



Sun, 30 Jun 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
The registry idea "sounds" like a big performance hit to me.

Cheers,
Randy

Quote:

> Just two {*filter*}s on the BTBB's reply below:

> First: We're doing Word here not Excel or atleast that's what I thought
> since I caught the post in the word97vba newsgroup.

> Second: Yep, I know that normally things in a collection start with 0,
> however, not always and not in this case. The first thing I tried was the
> following simple line of code:

>       MsgBox VBProject.VBComponents(0).Name

> and I got back the following error message:

>      Runtime Error 9: Subscript out of Range

> and sometimes:

>     Runtime Error 5941: The requested member of the collection does not
> exist.

> I also built a loop exactly as is illustrated by BTBB below and got the same
> error until I fixed it to the way it was in my original post.

> Another couple of instances I can specifically think of when a collection
> does not start at 0 are:

>     * Paragraphs
>     * BuiltInDocumentProperties
>     * Documents
>     * Sentences

> But the idea about the Registry is very sound.



> > First a simple rule in VB: .count for collections always start at 0 so the
> > line should have read
> > For i = 0 To VBProject.VBComponents.Count-1
> > Secondly as a possible solution couldn't you get each sub and function to
> > use the registry or similar to say what it is when it starts? Given that
> you
> > are using Excel a routine shouldn't be that difficult to fathom out to
> > automatically add a few lines of code to each sub and function that
> doesn't
> > already have them. Also if I remember correctly there is a Calls box in
> > Excel which could do what you ask if this is for debugging purposes.

> > Hope this helps

> > BTBB



> > > I can give you one clue. Each in a VB project becomes part of the
> > > VBProject collection.

> > > You can get the name of each module in your VBProject by looping through
> > > the components and getting the Name property for each
> component.Something
> > > like this:

> > > Sub printman()

> > > For i = 1 To VBProject.VBComponents.Count

> > > MsgBox VBProject.VBComponents(i).Name

> > > Next i

> > > End Sub

> > > Now, I have no idea how you can use this to get the name of the
> currently
> > > active module or the currently running sub/function, but I think this
> > > would be better than using the VBE object.


> > > > Hi all,

> > > > I would like to write some code to retrieve the name of the current
> > module
> > > > and the name of the current sub or function. For example, if I have a
> > module
> > > > called modTest and a function called blnTest, then I would like to get
> > those
> > > > names.

> > > > Function blnTest as boolean()
> > > >   Debug.Print <name of current module>
> > > >   Debug.Print <name of current sub/function>
> > > > End Function

> > > > I really don't know where to look... I tried
> > > > Workbooks("test.xls").VBProject.Name, but that gives me only the name
> of
> > the
> > > > project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name,
> > but
> > > > that gives me the name of the CodeModule that is currently on top of
> the
> > > > vb-editor. And that is not allways the module containing the current
> sub
> > or
> > > > function!

> > > > Does anybody have a clue? thanx in advance!!!

> > > > Marga.

> > > > http://www.*-*-*.com/ ~in004323



Mon, 01 Jul 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
Sorry but I saw the word Workgroups distinctly but it makes no difference as
the VB extensions required are the same. Anyway I never view other
newsgroups so that would account for my error. As for the collections not
starting at 0 that's news to me. I'll keep it in mind. But looking again I
notice that these are all Word collections, not VB collections so that makes
a bit of a difference.

As for the registry I meant that you would use it as a sort of one-line log
but looking at the problem a .txt file might be a better answer with exit
printing routines also.

Quote:
>>Given that
> you
> > are using Excel a routine shouldn't be that difficult to fathom out to
> > automatically add a few lines of code to each sub and function that
> doesn't
> > already have them.

Now to explain this. I was once shown a piece of code from a macro virus. It
was only for spreading it but it had a line something like:
print #1,vbcurrentproject.currentmodule.code.line(x)      'excuse the bad
call, it was at least 6 months ago
Using this you could add in lines to handle this, analysing the sub/function
name and then using it to add 3 lines(open,print,close) to the start and
end.

Hopefully this helps a bit more

BTBB


Quote:
> The registry idea "sounds" like a big performance hit to me.

> Cheers,
> Randy


> > Just two {*filter*}s on the BTBB's reply below:

> > First: We're doing Word here not Excel or atleast that's what I thought
> > since I caught the post in the word97vba newsgroup.

> > Second: Yep, I know that normally things in a collection start with 0,
> > however, not always and not in this case. The first thing I tried was
the
> > following simple line of code:

> >       MsgBox VBProject.VBComponents(0).Name

> > and I got back the following error message:

> >      Runtime Error 9: Subscript out of Range

> > and sometimes:

> >     Runtime Error 5941: The requested member of the collection does not
> > exist.

> > I also built a loop exactly as is illustrated by BTBB below and got the
same
> > error until I fixed it to the way it was in my original post.

> > Another couple of instances I can specifically think of when a
collection
> > does not start at 0 are:

> >     * Paragraphs
> >     * BuiltInDocumentProperties
> >     * Documents
> >     * Sentences

> > But the idea about the Registry is very sound.



> > > First a simple rule in VB: .count for collections always start at 0 so
the
> > > line should have read
> > > For i = 0 To VBProject.VBComponents.Count-1
> > > Secondly as a possible solution couldn't you get each sub and function
to
> > > use the registry or similar to say what it is when it starts? Given
that
> > you
> > > are using Excel a routine shouldn't be that difficult to fathom out to
> > > automatically add a few lines of code to each sub and function that
> > doesn't
> > > already have them. Also if I remember correctly there is a Calls box
in
> > > Excel which could do what you ask if this is for debugging purposes.

> > > Hope this helps

> > > BTBB



> > > > I can give you one clue. Each in a VB project becomes part of the
> > > > VBProject collection.

> > > > You can get the name of each module in your VBProject by looping
through
> > > > the components and getting the Name property for each
> > component.Something
> > > > like this:

> > > > Sub printman()

> > > > For i = 1 To VBProject.VBComponents.Count

> > > > MsgBox VBProject.VBComponents(i).Name

> > > > Next i

> > > > End Sub

> > > > Now, I have no idea how you can use this to get the name of the
> > currently
> > > > active module or the currently running sub/function, but I think
this
> > > > would be better than using the VBE object.


> > > > > Hi all,

> > > > > I would like to write some code to retrieve the name of the
current
> > > module
> > > > > and the name of the current sub or function. For example, if I
have a
> > > module
> > > > > called modTest and a function called blnTest, then I would like to
get
> > > those
> > > > > names.

> > > > > Function blnTest as boolean()
> > > > >   Debug.Print <name of current module>
> > > > >   Debug.Print <name of current sub/function>
> > > > > End Function

> > > > > I really don't know where to look... I tried
> > > > > Workbooks("test.xls").VBProject.Name, but that gives me only the
name
> > of
> > > the
> > > > > project. I also tried

Application.VBE.ActiveCodePane.CodeModule.Name,

- Show quoted text -

Quote:
> > > but
> > > > > that gives me the name of the CodeModule that is currently on top
of
> > the
> > > > > vb-editor. And that is not allways the module containing the
current
> > sub
> > > or
> > > > > function!

> > > > > Does anybody have a clue? thanx in advance!!!

> > > > > Marga.

> > > > > http://www.*-*-*.com/ ~in004323



Tue, 02 Jul 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module


Quote:

> As for the registry I meant that you would use it as a sort of one-line
log
> but looking at the problem a .txt file might be a better answer with exit
> printing routines also.

> >>Given that
> > you
> > > are using Excel a routine shouldn't be that difficult to fathom out to
> > > automatically add a few lines of code to each sub and function that
> > doesn't
> > > already have them.

> Now to explain this. I was once shown a piece of code from a macro virus.
It
> was only for spreading it but it had a line something like:
> print #1,vbcurrentproject.currentmodule.code.line(x)      'excuse the bad
> call, it was at least 6 months ago
> Using this you could add in lines to handle this, analysing the
sub/function
> name and then using it to add 3 lines(open,print,close) to the start and
> end.

> Hopefully this helps a bit more

> BTBB

Actually, this is exactly what I thought you meant which is why I thought it
was a pretty sound idea, I think some others viewing this are thinking about
the actual Windows Registry.

I think another thing that could work, if all you want to see is what
routine is running and when is to add some code that would put the name of
the module and sub in either the Title Bar or the Status Bar in each sub.
Something like

Constant strMod as String = [module name here in quotes]
Dim strSub as String

Sub Something ()

strSub = "Somthing"
Application.Caption = "Module: " & strMod & " Sub: " & strSub

' Whatever fancy code goes here

End Sub

Anyway, I wonder if we're just beating a dead horse here.

Lou



Wed, 03 Jul 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
Thanx a lot for all your help... Actually I wanted to use it for
errorhandling. If Word (or in my case Excel, but I don't believe that it
makes much of a difference in this), could see what module and what function
is running, then I could just copy the errorhandling. As long as I have to
type it somewhere (for instance at the top of the function), it's not just
simply copy and paste of errorhandling anymore. I thought it might be handy
to put the name of the module and function in the messagebox with the rest
of the error... especially if Word or Excel could just give me the names...
I'm still looking for the right solution, although I'm beginning to believe
that it's just not possible.

Thanx for all of your effort anyway...

Marga.



Thu, 04 Jul 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
Hello Marga,

I'm sorry that noone has a solution for this, because I too have wished to do
what you describe.

If you ever find a good solution please let me (us) know.

Thanks,
Randy

Quote:

> Thanx a lot for all your help... Actually I wanted to use it for
> errorhandling. If Word (or in my case Excel, but I don't believe that it
> makes much of a difference in this), could see what module and what function
> is running, then I could just copy the errorhandling. As long as I have to
> type it somewhere (for instance at the top of the function), it's not just
> simply copy and paste of errorhandling anymore. I thought it might be handy
> to put the name of the module and function in the messagebox with the rest
> of the error... especially if Word or Excel could just give me the names...
> I'm still looking for the right solution, although I'm beginning to believe
> that it's just not possible.

> Thanx for all of your effort anyway...

> Marga.



Thu, 04 Jul 2002 03:00:00 GMT  
 Retrieve name of current sub/function and name of current module
I have a generic error handler that does a similar operation as described in
an earlier post in this thread.  On an error condition i send control to a
generic handler that i put in nearly every function.  Again, as previously
posted i have 2 form/module level variables containing the name of the
module and the name of the current func/sub being executed.

A trick i found that makes this useful is too only set the name of the
operating sub once the error has been tripped.  This prevents confusion that
can happen when you set the name of your active function at the TOP of your
sub and then a subsequent sub/func is called (resetting the current sub/func
variable) and an error occurs in your original function when control is
passed back.  This would result in the current sub/func variable containing
the wrong name of the current sub/func when the error is logged.

This might be off topic for what your purposes are, but i have found this
invaluable when combining it with some email code in my generic error
handler.  That is my instant notification any time one of my programs gets
an error it cant handle gracefully.  My users (all over a cross-Canada WAN)
are often suprised when i call them about 30 seconds after my program has
given them an error and i know (generally) what has happened and can use
them to discover what exactly has caused the condition to arise.

Const sMod = "modWhatever"
Private sFunc$

Private Sub FiveAssedMonkey(iMonkey)
    On Error Goto Handler
        ...
        Some code
        ...
    Exit Sub
Handler:
    sFunc = "FiveAssedMonkey"
    ' If my handler determines the error is recoverable then it will pop-up
and
    '    recommend to the user to try to continue execution of the program.
    '    It gives them a dialogue with the choice to exit gracefully or
attempt to continue
    if MyGenericErrorHandler(sMod, sFunc) = true then
        Resume Next
    else
        Call EndGracefully
    end
end Sub


Quote:
> Hello Marga,

> I'm sorry that noone has a solution for this, because I too have wished to
do
> what you describe.

> If you ever find a good solution please let me (us) know.

> Thanks,
> Randy


> > Thanx a lot for all your help... Actually I wanted to use it for
> > errorhandling. If Word (or in my case Excel, but I don't believe that it
> > makes much of a difference in this), could see what module and what
function
> > is running, then I could just copy the errorhandling. As long as I have
to
> > type it somewhere (for instance at the top of the function), it's not
just
> > simply copy and paste of errorhandling anymore. I thought it might be
handy
> > to put the name of the module and function in the messagebox with the
rest
> > of the error... especially if Word or Excel could just give me the
names...
> > I'm still looking for the right solution, although I'm beginning to
believe
> > that it's just not possible.

> > Thanx for all of your effort anyway...

> > Marga.



- Show quoted text -

Quote:
> I can give you one clue. Each in a VB project becomes part of the
> VBProject collection.

> You can get the name of each module in your VBProject by looping through
> the components and getting the Name property for each component.Something
> like this:

> Sub printman()

> For i = 1 To VBProject.VBComponents.Count

> MsgBox VBProject.VBComponents(i).Name

> Next i

> End Sub

> Now, I have no idea how you can use this to get the name of the currently
> active module or the currently running sub/function, but I think this
> would be better than using the VBE object.


> > Hi all,

> > I would like to write some code to retrieve the name of the current
module
> > and the name of the current sub or function. For example, if I have a
module
> > called modTest and a function called blnTest, then I would like to get
those
> > names.

> > Function blnTest as boolean()
> >   Debug.Print <name of current module>
> >   Debug.Print <name of current sub/function>
> > End Function

> > I really don't know where to look... I tried
> > Workbooks("test.xls").VBProject.Name, but that gives me only the name of
the
> > project. I also tried Application.VBE.ActiveCodePane.CodeModule.Name,
but
> > that gives me the name of the CodeModule that is currently on top of the
> > vb-editor. And that is not allways the module containing the current sub
or
> > function!

> > Does anybody have a clue? thanx in advance!!!

> > Marga.

> > http://www.euronet.nl/~in004323



Fri, 05 Jul 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Obtaining Current Sub/Function Name

2. (Q) Getting current Sub/Function name (or Object Browser functionality)

3. getting the name of current function/SUb

4. How to get the current function/sub Name

5. Retrieving the name of the current function or subroutine

6. Get Name of Current Sub Running

7. Get the name of the current SUB

8. Returning the name of the current sub

9. Retrieving the profile name used by the current user in Outlook

10. how to retrieve the name of the current folder

11. How to retrieve the current domain name under WIN95

12. Name of current module and line of code

 

 
Powered by phpBB® Forum Software