Reputation: 17
I created a two dimensional string array. I set up a for loop to take a string from the user and search the first column of the array. I want to use the corresponding string from the second column of the array to call a method. However, I can't figure out how to use a string to call a sub or the method of a class in Outlook VBA. Any suggestions?
Upvotes: 0
Views: 254
Reputation: 1239
You can use the call by name function using the name of an objects method at runtime. The simple example below creates an object of a class and an array and then calls methods named in the second elements of the array.
In the Class Module:
Sub Procedure1()
MsgBox "I'm procedure1"
End Sub
Sub Procedure2()
MsgBox "I'm Procedure2"
End Sub
Sub Procedure3(ByVal sName As String)
MsgBox "Procedure 3 here " & sName
End Sub
In the standard module:
Sub Main()
Dim myArray(1 To 2, 1 To 3) As String
Dim oClass1 As Class1
Set oClass1 = New Class1
'oClass1.Procedure1
myArray(1, 1) = "Procedure1"
myArray(1, 2) = "Procedure2"
myArray(1, 3) = "Procedure3"
Call CallByName(oClass1, myArray(1, 1), VbMethod)
Call CallByName(oClass1, myArray(1, 2), VbMethod)
Call CallByName(oClass1, myArray(1, 3), VbMethod, "Graham")
Call CallByName(oClass1, myArray(1, 3), VbMethod, "user3299464")
End Sub
Upvotes: 1