none
Passing a subroutine as an argument to another subroutine RRS feed

  • Question

  • I have created a simple subroutine that uses the File System Object to recursively walk a directory tree and do some operation on all the files in the tree.  This works fine but I'd like to generalize this function by passing it a subroutine to do the file processing.

    In VBA for word can you pass a subroutine as a parameter to another subroutine. 

    Thanks,

    Harry

    Monday, December 15, 2014 3:32 PM

Answers

  • Not directly.

    In some cases it may be worth simulating this using Class Modules and Interfaces. e.g.

    You have a Class Module called IMysub that contains

    Sub Doit(s1 As String, s2 As String)
    End Sub

    Then you have a Class Module mysub1 where Doit displays the 1st string:

    Implements IMysub
    Private Sub IMysub_Doit(s1 As String, s2 As String)
    MsgBox s1
    End Sub

    and a Class Module mysub2 where Doit displays the 2nd string:

    Implements IMysub
    Private Sub IMysub_Doit(s1 As String, s2 As String)
    MsgBox s2
    End Sub

    Then when you call your subroutine, you can use one Doit or the other by passing the approprate object type:

    Sub tryIMysub(s1 As String, s2 As String, mysub As IMysub)
    mysub.Doit s1 s2
    End Sub

    and you can test that via

    Sub testTryIMysub()
    Dim x as IMysub
    Set x = New mysub1
    tryIMysub "string one", "string two", x
    Set x = Nothing
    Set x = New mysub2
    tryIMysub "string one", "string two", x
    Set x = Nothing
    End Sub


    Peter Jamieson

    Wednesday, December 17, 2014 12:54 PM

All replies

  • Let's say that you have a procedure

    Sub ProcessFile(strFilePath As String)
        ' Code to do something with the file at strFilepath goes here
        ...
    End Sub

    You can call this from another subroutine (for example the one that traverses a folder tree) as

        Dim strFile As String
        ...
        ...
        strFile = ... ' get path and filename from your loop
        Call ProcessFile(strFile)
        ...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, December 15, 2014 3:57 PM
  • Not directly.

    In some cases it may be worth simulating this using Class Modules and Interfaces. e.g.

    You have a Class Module called IMysub that contains

    Sub Doit(s1 As String, s2 As String)
    End Sub

    Then you have a Class Module mysub1 where Doit displays the 1st string:

    Implements IMysub
    Private Sub IMysub_Doit(s1 As String, s2 As String)
    MsgBox s1
    End Sub

    and a Class Module mysub2 where Doit displays the 2nd string:

    Implements IMysub
    Private Sub IMysub_Doit(s1 As String, s2 As String)
    MsgBox s2
    End Sub

    Then when you call your subroutine, you can use one Doit or the other by passing the approprate object type:

    Sub tryIMysub(s1 As String, s2 As String, mysub As IMysub)
    mysub.Doit s1 s2
    End Sub

    and you can test that via

    Sub testTryIMysub()
    Dim x as IMysub
    Set x = New mysub1
    tryIMysub "string one", "string two", x
    Set x = Nothing
    Set x = New mysub2
    tryIMysub "string one", "string two", x
    Set x = Nothing
    End Sub


    Peter Jamieson

    Wednesday, December 17, 2014 12:54 PM