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

标签 vba excel

当接口(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/

相关文章:

excel - 为什么我的 Sub 不能激活我想要的工作表?

javascript - 我如何使用 Excel vba 从 javascript/CSS 网站中选择一个元素?

vba - Word vba - 如何在模块中存储字符串值?

excel - SUMIFS excel 公式中省略空格

vba - 如何在 VBA 中动态调整数组的大小?我不断收到下标和索引问题

excel - 错误处理-将#NUM转换为零

excel - 读取 Excel Open XML 会忽略空白单元格

c# - 在 vb.net winform 应用程序中将数据表保存到 excel 表

c# - '文件已损坏,无法打开' OpenXML

excel - VBA 单元格值问题以及是否