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

    Named arguments in user-defined functions

    Is it possible for us developers to write Word97/VBA functions that make use of named arguments?

    I'm developing a function with a slew of optional arguments. I started off with about fifteen OPTIONAL parameters, some with defaults, but this quickly gets out of hand.

    I'd like to be able to write my function so that I can call it like this:
    <pre> Call FDir(GreavesSort:=TRUE)
    </pre>

    where I have coded a function FDir and within that function detect the presence of the parameter "GreavesSort" and, of course, its specified value "True".


    I figure I could do it by enclosing my arguments in strings and analysing the strings myself:
    <pre> call FDir("GreavesSort:=TRUE")
    </pre>


    but I'd rather not do that. (That's not true, I'd love to do that, but there's other things I'd love to do more)

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Named arguments in user-defined functions

    It works in Office 2000, even if the arguments are completely out of order:

    Sub testNamedArg()
    MsgBox WildReplace(strReplace:="-", strFind:=" ", strExpression:="abc 123")
    End Sub

    (I think I posted the WildReplace() function twice; the more recent one was better for some reason. Both require the presence of IE5.x.)

  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: Named arguments in user-defined functions

    Thank you. I have stripped it down to demonstrate that it does indeed do what I want.

    Now, what do YOU want? An honorable mention in my replacement function for the VBA DIR function? (grin!)

    <pre>Public Function WildReplace(strExpression As String, strFind As String, _
    strReplace As String, Optional bolReplaceAll As Boolean = True, _
    Optional bolCaseSensitive As Boolean = False) As String
    If (strExpression = vbNullString) Or (strFind = vbNullString) Then
    WildReplace = strExpression
    Exit Function
    End If
    MsgBox strExpression
    MsgBox strFind
    End Function

    Sub testNamedArg()
    Call WildReplace(strReplace:="1", strFind:="2", strExpression:="3")
    End Sub
    </pre>



    Those search tags are something else, aren't they!

Posting Permissions

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