VBA:使用implements关键字进行条件编译

cru*_*nch 6 excel vba excel-vba

是否可以将编译器条件常量与"implements"关键字一起使用,其中接口位于加载项中?

我在工作簿中的类模块中有以下内容,我们称之为book1:

#Const Condition1 = 0 ''will be replaced with 1 when add-in is opened
#if Condition1 then
    Implements myAddIn.iInterfaceFoo
#End if
Run Code Online (Sandbox Code Playgroud)

我将加载项myAddIn列为参考(即在工具 - >参考文献中...).

我成功地使用了加载项中其他类的接口,但现在我想直接在我的工作簿book1中调用该接口.只要加载项是打开的,当我编译book1(即Debug - > Compile VBAProject)时,它就会成功编译.

但是,当我尝试在加载项关闭的情况下编译book1时,我收到错误

Compile error: User-defined type not defined
Run Code Online (Sandbox Code Playgroud)

这正是我想要避免的 - 否则如果加载项丢失(例如在别人的计算机上),电子表格本身仍然可以工作.

Zvi*_*ler 1

我查了很多资料,但没有找到解决该问题的好方法。

所以我在另一个文件中编写了有问题的函数,如果我需要它,我会像这样激活它:

在模块中的 sp.mdb 上:

Public Function soap30object() As Object
Set soap30object = New SoapClient30
End Function
Run Code Online (Sandbox Code Playgroud)

在主文件上:

Public Sub soap30object()
Dim ob As Object
Dim appAccess As New Access.Application
appAccess.OpenCurrentDatabase ("c:\sp\sp.mdb")
Set ob = appAccess.Run("soap30object")
End Sub
Run Code Online (Sandbox Code Playgroud)

玩得开心!


另一种解决方案

替换运行时模块中的代码...

     Public Sub replacemodel(mdlname As String, fnd As String, cngto As String)
        Dim toi As Long, oldlin As String, i As Long, firstchr As String, linnewnum As Long, last_ As Boolean
        Dim frm As Form,mdl As Module 
        DoCmd.OpenForm mdlname, acDesign
        Set mdl = Forms(mdlname).Module
        toi = mdl.CountOfLines
        With mdl
            For i = 1 To toi
                linnewnum = i
                oldlin = .lines(i, 1)
                If InStr(oldlin, fnd) <> 0 Then
                    oldlin = Replace(oldlin, fnd, cngto)
                    .ReplaceLine i, oldlin
                    goto nexx
                End If
            Next i
        End With
nexx:
        DoCmd.Close acForm, mdlname, acSaveYes
        Set mdl = Nothing
        'All variables reset when you edit modul on 
        msgbox "Program will restart now..."
        DoCmd.Quit acQuitSaveAll
    end Sub
Run Code Online (Sandbox Code Playgroud)