CallByName syntax

1.2k views Asked by At

I want to process incoming mails.

My code has a lengthy conditional statement, to determine the category to which each incoming email belongs.

Based on the result of this conditional statement, the code is supposed to read the appropriate property of the MailItem object.

I want to use CallByName function to achieve this.

x = CallByName(itm, PAN_Source, VbGet)

Where 'itm' is the MailItem Object, 'PAN_Source' variable contains the string, which refers to the specific property from 'itm'.

For example, if an incoming mail has an attachment, then 'PAN_Source' is given the value "Attachments.Item(1).DisplayName". I want CallByName(itm, PAN_Source, VbGet) to return itm.Attachments.Item(1).DisplayName

The code just stops execution at this stage.

2

There are 2 answers

2
Eugene Astafiev On

The CallByName function accepts the following arguments:

  • The name of the object on which the function will be executed.
  • A string expression containing the name of a property or method of the object.
  • A constant of type vbCallType representing the type of procedure being called.
  • Arguments (optional).

I'd try to use the following instead:

Call CallByName("itm", PAN_Source, VbGet)

You may find the Getting Started with VBA in Outlook 2010 article helpful.

0
Marcelo Scofano Diniz On

It is an old question, I know, but for the sake of correctness: VBA's CallByName does not accept a string as

"The name of the object on which the function will be executed."

In some places the MSDN example is distorted to a plainly wrong one. It has to be the object per si. So, instead of:

Call CallByName("itm", PAN_Source, VbGet)

Do that:

Call CallByName(itm, "NameMethodThatUsesString_PAN_Source" , VbGet, PAN_Source)

PAN_Source has to be supplied at the optional Args().

As a bonus, if it returns something as a Function (vbMethod), receive it with a Variant:

auxVar = CallByName (itm, "NameMethodThatUsesString_PAN_Source" , VbGet, PAN_Source)

I use it mostly to iterate over some Collections to SortIt() or retrieve values - or both - through Property Get or Set, in a Sorting algorithm, for example:

 ...

        If IsMissing(CallByNameArg0) Then
            If VarType(CallByName(this, SortPropertyName, VbGet)) = vbObject Then
                Set thisValue = CallByName(this, SortPropertyName, VbGet)
            Else
                thisValue = CallByName(this, SortPropertyName, VbGet)
            End If
        Else
            If VarType(CallByName(this, SortPropertyName, VbGet)) = vbObject Then
                Set thisValue = CallByName(this, SortPropertyName, VbGet, CallByNameArg0)
            Else
                thisValue = CallByName(this, SortPropertyName, VbGet, CallByNameArg0)
            End If
        End If
 ...

Where this is the Object that we want to CallBack and CallByNameArg0 is a Variant (to allow IsMissing(CallByNameArg0 ))

And in case that someone try to CallByName against Standard Module, it will not function; instead, try Application.Run