什么是VBA中的公共对象模块? [英] What is a public object module in VBA?

查看:181
本文介绍了什么是VBA中的公共对象模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VBA中尽可能接近函数指针/抽象类.

我有一个名为 VerificationManager 的类,并验证了两个电子表格中的一堆单元格是否匹配.这将根据使用的信息和电子表格以不同的方式完成.

我希望能够通过使用 Application.Run 函数在字符串中指定要调用的方法来使代码可重复使用.因此,我可以重写更改的功能.

现在,如果我使用的是Java或C#,我将能够扩展一个抽象类并将其内部重写为该函数.如果我使用的是JavaScript,则可以将函数存储在变量中,然后将变量传递给类并从那里进行调用.

在我的类中,我有一个名为"verificationModule"的公共属性,该属性设置为要调用的函数的名称.

  Sub VerifyWorkLocations(将empLoc作为EmployerLocation)...对于i = 0到empLoc.numOfEmpApplication.Run验证模块,empLoc.taxdescmatch,empLoc.employees(i)接下来我...结束子 

但是,当我尝试调用 Application.Run 时,出现以下错误:

编译错误:

仅在公共对象模块中定义的用户定义类型可以是强制于变体或来自变体,或传递给后期绑定函数"

我已经尝试过将用户定义的类型放置在 Class Module 中,但基本上它说类模块对于类型来说是错误的位置.

解决方案

错误来自成熟的VB,您可以在其中创建ActiveX dll项目,在其中创建公共类,然后将UDT放入该类中.>

在VBA中,当您需要强制使用变体或来自变体时,可以使用类而不是UDT.

因此只需声明一个类,其中包含您的UDT中的所有字段,然后删除UDT.

或者,在VB6中创建一个仅包含类型声明的DLL,然后从VBA引用该dll.或者,如果您对IDL感到满意,只需直接创建TLB文件即可.

I'm trying to get as close to function pointers / abstract classes as I can in VBA.

I have a class called VerificationManager and verifies a bunch of cells in a couple of spreadsheets match up. This will be done in different ways depending on the information and spreadsheets being used with it.

I'd like to be able to make the code reusable by specifying a method to be called in a string using the Application.Run function. So I can rewrite the function that changes.

Now if I was using Java or C# I would be able to extend an abstract class and rewrite the internals to the function. If I was using JavaScript I could store a function in a variable and pass the variable to the class and call it from there.

Inside my class I have a public property called "verificationModule" which I set to the name of the function I want it to call.

Sub VerifyWorkLocations(empLoc As EmployerLocation)
...
    For i = 0 To empLoc.numOfEmp
        Application.Run verificationModule, empLoc.taxdescmatch, empLoc.employees(i)
    Next i
...
End Sub

However, when I try to call Application.Run I receive the following error:

Compile Error:

"Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions"

I already tried placing my User Defined Types in a Class Module but it basically said that a class module was the wrong place for a type.

解决方案

The error comes from full-fledged VB, where you can create an ActiveX dll project, create a public class there and put a UDT into that class.

In VBA, you use classes instead of UDTs when you need to coerce to or from a variant.

So just declare a class with all the fields you have in your UDT, and delete the UDT.

Alternatively, create a DLL in VB6 that would only contain the declaration of the type, and reference that dll from VBA. Or, if you're comfortable with IDL, just create a TLB file directly.

这篇关于什么是VBA中的公共对象模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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