有没有办法在VBA中获得枚举? [英] Is there a way to get the enums in VBA?

查看:470
本文介绍了有没有办法在VBA中获得枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在VBA中获取枚举?类似于此示例(对于C#,但对于VBA)?

 使用系统; 

类EnumsExampleZ
{
私有枚举SiteNames
{
SomeSample = 1,
SomeOtherSample = 2,
SomeThirdSample = 3
}

static void Main()
{
Type enumType = typeof(SiteNames);
string [] enumName = enumType.GetEnumNames();

for(int i = 0; i {
Console.WriteLine(enumName [i]);
}
}
}

我们说以下:

 枚举FruitType 
苹果= 1
橙色= 2
李子= 3
结束枚举

我们如何在即时窗口中显示以下内容:

 苹果
橙色
李子


解决方案

使用VBIDE可扩展性库自己解析VBA代码会显得不错&首先要很简单,然后您将遇到一些极端情况,并很快意识到您需要实际实现VBA规范的这一部分,以便正确并成功地解析在VBA中定义枚举的所有可能方式。



我会选择 ,但请注意,那时该API仍处于试验阶段,非常容易更改(欢迎提供功能要求!),因此,我不建议将其用于玩具项目之外。


Is there a way to get the enums in VBA? Something like this example for C#, but for VBA?

using System;

class EnumsExampleZ
{
    private enum SiteNames
    {
        SomeSample = 1,
        SomeOtherSample = 2,
        SomeThirdSample = 3
    }

    static void Main()
    {
        Type enumType = typeof(SiteNames);
        string[] enumName = enumType.GetEnumNames();

        for (int i = 0; i < enumName.Length; i++)
        {
            Console.WriteLine(enumName[i]);
        }
    }
}

Lets say we have the following:

Enum FruitType
    Apple = 1
    Orange = 2
    Plum = 3
End Enum

How can we display on the immediate window these:

Apple
Orange
Plum

解决方案

Parsing the VBA code yourself with the VBIDE Extensibility library is going to appear nice & simple at first, and then you're going to hit edge cases and soon realize that you need to actually implement that part of the VBA spec in order to properly and successfully parse every possible way to define an enum in VBA.

I'd go with the simple solution.

That said Rubberduck is doing pretty much exactly that, and exposes an experimental COM API that allows you to enumerate all declarations (and their references) in the VBE, effectively empowering your VBA code with reflection-like capabilities; as of 2.0.11 (the latest release), the code would look something like this:

Public Enum TestEnum
    Foo
    Bar
End Enum

Public Sub ListEnums()
    With New Rubberduck.ParserState
        .Initialize Application.VBE
        .Parse
        Dim item As Variant
        For Each item In .UserDeclarations
            Dim decl As Rubberduck.Declaration
            Set decl = item
            If decl.DeclarationType = DeclarationType_EnumerationMember Then
                Debug.Print decl.ParentDeclaration.Name & "." & decl.Name
            End If
        Next
    End With
End Sub

And in theory would output this:

TestEnum.Foo
TestEnum.Bar

However we (ok, I did) broke something around the 2.0.9 release, so if you try that in 2.0.11 you'll get a runtime error complaining about an invalid cast:

That should be is an easy fix that we'll patch up by 2.0.12, but note that at that point the API is still experimental and very much subject to change (feature requests are welcome!), so I wouldn't recommend using it for anything other than toy projects.

这篇关于有没有办法在VBA中获得枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆