views:

143

answers:

1

Am trying create of PoC for exposing/invoking various .NET objects from COM clients. The .NET library contains some classes and Enums.

Am able to successfully access the classes in VBScript but not able to access the Enums. I know that Enums are value types and hence 'CreateObject' wont work in this case.

But am able to access the same Enum in VBA code.

Questions:

  • How can I access the enums in VBScript?

  • Why does the behaviour differ in the two COM clients? If VBA object browser can see the enum, why cant VBScript allow me to create one?

.NET

  [ComVisible(true)]
[GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]        
public enum Currency
{        
    GBP = CurrencyConvertorBL.CurrencyConvertorRef.Currency.GBP,        
    USD = CurrencyConvertorBL.CurrencyConvertorRef.Currency.USD,        
    INR = CurrencyConvertorBL.CurrencyConvertorRef.Currency.INR,
    AUD = CurrencyConvertorBL.CurrencyConvertorRef.Currency.AUD
}

VBA

    Private Function ConvertCurrency(fromCurrency As Currency, 
toCurrency As Currency) As Double

VBScript ???

Set currencyConvertorCCY = CreateObject("CurrencyConvertorBL.Currency")

Thanks in advance.

A: 

Currency is a built-in datatype in VBA, a numeric one. You have to prefix your enum with your typelib name not to be ambiguous for the compiler i.e. fromCurrency As MyProject.Currency.

For VBScript try this article: How Can I Access a Type Library From Within a Script?

wqw
The first one is not a problem. I've been able to access the correct 'Currency' using the object browser.Second point: the problem is not in accessing a tlb in script but an Enum from the tlb. From the link you mentioned **Second, notice that -- in the very first line of the script -- we explicitly assign the value 2 to the constant ForWriting. That’s because VBScript doesn’t have access to type libraries; therefore, we have to tell our script that the value of ForWriting is equal to 2.**I want to reuse the Enums declared in the tlb.
Codex
Read down the page: "The problem with WSF files is that the advantages they add (like providing access to a type library) are often outweighed by the disadvantages of having to deal with wrapping your script up in a bunch of XML tags"
wqw