Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function as sub/macro (word97sr2)

    Something I learned this morning - I can treat a Function as a Sub for purposes of testing - providing that the function uses no parameters.


    <pre>Public Function strGrabEntireParagraph() As Stringblah blah blah
    End Function
    </pre>


    If you've met me before you know I usually embed a small TEST Sub as a comment block at the end of the procedure for purposes of regression testing, illustration etc.

    I wasn't thinking this morning, and just started single-stepping through my function. It was over half an hour later that I realised I'd forgotton to write a SUB to test it.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Function as sub/macro (word97sr2)

    I assume that by treating a "function as a sub", you mean calling a function without assigning its return value to a variable?
    Charlotte

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function as sub/macro (word97sr2)

    > calling a function without assigning

    Yes, but more specifically, in VBE, by selecting any line within the function procedure and then clicking on the single-step button on the toolbar.

    When I started writing Word/VBA, my procedures were functions that took arguments and incidentally returned results. For these functions I needed to have a short TEST Sub associated with the function in order to supply appropriate test argument values to the function being tested. It is now a habit.

    More recently I started implementing procedures as Functions for those procedures-without-arguments that I didn't want to show up as macros in the Word interface (Tools, Macro, Macros ... Run).

    So my mind is/was fixed on "You gotta have a Test Sub in order to test a Function". Now I find it ain't so!



    I should probably continue my standard, such as it is, of including a brief TEST example sub with each procedure, be it a function or a subroutine-with-arguments.


    It just struck me as a piece of knowledge.

    I know we had a thread on this early last year (March 2001 or thereabouts)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •