首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA:带有implements关键字的条件编译

VBA:带有implements关键字的条件编译
EN

Stack Overflow用户
提问于 2015-03-02 16:54:25
回答 1查看 555关注 0票数 7

是否可以使用编译器条件常量和“实现”关键字(接口在外接程序中)一起使用?

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

代码语言:javascript
复制
#Const Condition1 = 0 ''will be replaced with 1 when add-in is opened
#if Condition1 then
    Implements myAddIn.iInterfaceFoo
#End if

我将外接程序myAddIn作为引用列出(即在Tools ->引用中.)。

我正在成功地将接口与外接程序中的其他类一起使用,但现在我想在工作簿book1中直接调用该接口。只要外接程序是打开的,当我编译book1 (即调试->编译VBAProject)时,它就会成功编译。

但是,当我试图用加载项关闭编译book1时,我会得到错误。

代码语言:javascript
复制
Compile error: User-defined type not defined

这正是我试图避免的--否则如果外接程序丢失(例如在其他人的计算机上),电子表格本身仍能工作。

EN

回答 1

Stack Overflow用户

发布于 2015-04-27 07:19:10

我看了很久,没有找到解决那个问题的好办法。

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

在模块中的sp.mdb上:

代码语言:javascript
复制
Public Function soap30object() As Object
Set soap30object = New SoapClient30
End Function

在主文件上:

代码语言:javascript
复制
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

玩得开心!

另一种解决办法

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

代码语言:javascript
复制
     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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28815255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档