Skip to main content

CallByName function

Table of contents
  1. Syntax
  2. Remarks
  3. Example

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

Syntax

CallByName (objectprocnamecalltype, [args()]_)

The CallByName function syntax has these named arguments:

Part Description
object Required: Variant (Object). The name of the object on which the function will be executed.
procname Required: Variant (String). A string expression containing the name of a property or method of the object.
calltype Required: Constant. A constant of type vbCallType representing the type of procedure being called.
args() Optional: Variant (Array).

Remarks

The CallByName function is used to get or set a property, or invoke a method at run time by 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

Example

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 "Move", and the name of the method to invoke. 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
    Else
        CallByName Command1, Label1.Caption, VbMethod, 500, 500
    End If
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>