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

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

问题描述

有没有办法在 VBA 中获取枚举?类似于 C# 的这个示例,但适用于 VBA?

使用系统;类 EnumsExampleZ{私有枚举 SiteNames{一些样本 = 1,SomeOtherSample = 2,某个第三个样本 = 3}静态无效主(){类型 enumType = typeof(SiteNames);string[] enumName = enumType.GetEnumNames();for (int i = 0; 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天全站免登陆