Search code examples
vbscriptmfcole-automation

VBScript: How do I invoke a function which is defined with a vowel in the Dispatch Map


I have following VBScript-Script that creates an Microsoft COM Object.

set foo = CreateObject("Bar.ProgId")
foo.Einfügen()

The implementation of that COM Object is in the Dynamic Link Library Foo.dll which is using the Microsoft Foundation Framework (MFC) and especially the mechanism of Dispatch Maps.

The Dispatch Maps show like this

BEGIN_DISPATCH_MAP(Foo, CCmdTarget)
[...]
DISP_FUNCTION(Foo, "Einfügen", Insert, VT_I2, VTS_NONE)
[...]
END_DISPATCH_MAP()

So far so good. When I create such a COM-Object in a VBA-Environment for example in a Excel Application, the invocation of foo.Einfügen is successful. Other than in the VBScript-Script.

I recognized that OLE Automation is applied in VBA as well as in VBScript. But when I run the VBScript-Script I got the error

Error: unknow character; Code 8000A0408;
Source: Compile failure in Microsoft VBScript

First I thought that the encoding in the VBScript-Script is wrong because of the character ü but I verified that it is ANSI encoded. I also verified that the Windows Code Page on my system is Windows-1252 the Western Latin Character Set.

If I change Einfügen to Einfuegen in the Dispatch Map I assert that the VBScript-Script is working.

So my question is, why the behavior of the invocation of foo.Einfügen is different in VBScript than in VBA? What is the reason for this behavior?

For me the workaround is that I append a second DISP_FUNCTION Entry in the Dispatch Map that dispatches Einfuegen to the function Insert. In the VBScript-Script I invoke foo.Einfuegen and in VBA still foo.Einfügen.


Solution

  • Naming conventions.
    Unlike its bigger brothers VBA and VB6, it's a syntax error in VBScript. VBScript compiler expects single-byte characters for expressions.

    Have a look at:

    Syntax error (VBScript)

    Invalid character (VBScript)

    You have to write expressions containing unsupported characters enclosed in brackets.

    set foo = CreateObject("Bar.ProgId")
    foo.[Einfügen]() 'this will work