CallByName Function

Executes a method of an object, or sets or returns a property of an object.


CallByName ( object , procname, calltype [, args()])

The CallByName function syntax has these arguments:




Required. Variant (Object). The name of the object on which the function will be executed.


Required. Variant (String). A string expression containing the name of a property or method of the object.


Required. Constant. A constant of type vbCallType representing the type of procedure being called.

args ()

Optional. Variant (Array).


The CallByName function is used to get or set a property, or to invoke a method at run time using a string name.

In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:

CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100


Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

This example uses the CallByName function to invoke the Move method of a command button.

The example also uses a form (Form1) with a button (Command1), and a label (Label1). When the form is loaded, the Caption property of the label is set to the name of the method to invoke, in this case, "Move". When you click the button, the CallByName function invokes the method to change the location of the button.

Option Explicit
Private Sub Form_Load()
Label1.Caption = "Move"' Name of Move method.
End Sub
Private Sub Command1_Click()
If Command1.Left <> 0 Then
CallByName Command1, Label1.Caption, vbMethod, 0, 0
CallByName Command1, Label1.Caption, vbMethod, 500, 500
End If
Share Facebook Facebook Twitter Twitter Email Email

Was this information helpful?

Great! Any other feedback?

How can we improve it?

Thank you for your feedback!