VBA:在预处理时访问注册表 [英] VBA: Access to registry while preprocessing

查看:225
本文介绍了VBA:在预处理时访问注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个VBA项目中有条件地编译代码,条件取决于一些注册表项。这在VBA是不可能的吗?

I want to conditionally compile code in one VBAproject, with a condition that depends on some registry-entry. Is this somehow possible in VBA?

我知道在VBA有一些简单的预处理可能性,但我不能看到是否可能以某种方式访问​​注册表,而预处理。或者也许在编译之前检查注册表的一些其他可能性。

I know that there are some simple preprocessing possibilites in VBA, but I can not see if it is possible to somehow access the registry while preprocessing. Or Maybe some other possibility to check the registry before compiling.

由于我得到一个编译错误,因为一些缺少引用(因此缺少类对象),我的目标是检查编译前的注册表。

Since I get a compile error because of some missing reference(and thus missing class object), I aim to check the registry before compiling.

P.s。我只想读取注册表项。

P.s. I only want to read registry-entries.

例如。如何到达debug.print在下面,即避免编译错误。

As an example. How to reach debug.print in the following, i.e. avoiding compile errors.

sub sub1()
   dim testobj as new nonexistingobject
   sub2 testobj
   debug.print "Arrived at this point"
end sub

sub sub2( byref testobj as nonexistingobject)
   *do some stuff with testobj*
end sub


推荐答案

使用早期绑定( Dim obj as myObject ),使用 CreateObject 的晚期绑定。这样,您将能够处理对象不存在的情况:

Instead of using early binding (Dim obj as myObject), use late bindings with CreateObject. This way you will be able to handle the case where the object doesn't exists:

Sub test()
    Dim obj As Object
    On Error Resume Next
    obj = CreateObject("myObject")
    if Err then Exit Sub 'if the object wasn't found exit the function
    on error goto 0 'set back the error handling to its previous state

    'rest of the code
End Sub

这篇关于VBA:在预处理时访问注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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