当接口(interface)位于加载项中时,是否可以将编译器条件常量与“implements”关键字一起使用?
我的工作簿中的类模块中有以下内容,我们将其称为 book1:
#Const Condition1 = 0 ''will be replaced with 1 when add-in is opened
#if Condition1 then
Implements myAddIn.iInterfaceFoo
#End if
我将加载项 myAddIn 列为引用(即在工具 -> 引用...中)。
我已成功将该接口(interface)与加载项中的其他类一起使用,但现在我想直接在我的工作簿 book1 中调用该接口(interface)。只要加载项打开,当我编译 book1(即调试 -> 编译 VBAProject)时,它就会成功编译。
但是,当我尝试在加载项关闭的情况下编译 book1 时,出现错误
Compile error: User-defined type not defined
这正是我想要避免的 - 否则,如果加载项丢失(例如在其他人的计算机上),电子表格本身仍然可以工作。
最佳答案
我查了很多资料,但没有找到解决该问题的好方法。
所以我在另一个文件中编写了有问题的函数,如果我需要它,我会像这样激活它:
在模块中的 sp.mdb 上:
Public Function soap30object() As Object
Set soap30object = New SoapClient30
End Function
在主文件上:
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
玩得开心!
<小时/>另一种解决方案
在运行时替换Modul中的代码...
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
关于VBA:使用implements关键字进行条件编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815255/