2015-03-02 16 views
5

是否有可能將編譯器條件常量與「implements」關鍵字一起使用,其中接口位於加載項中?VBA:條件編譯與執行關鍵字

我有一個類模塊在我的工作簿下面,讓我們把它BOOK1:

#Const Condition1 = 0 ''will be replaced with 1 when add-in is opened 
#if Condition1 then 
    Implements myAddIn.iInterfaceFoo 
#End if 

我有外接列爲參考myAddIn(即工具 - >參考.. )。

我成功地在加載項中使用其他類的接口,但現在我想直接在工作簿book1中調用接口。只要加載項是打開的,當我編譯book1時(即Debug - > Compile VBAProject),它會成功編譯。

然而,當我嘗試編譯BOOK1與外接封閉的,我得到的錯誤

Compile error: User-defined type not defined 

這正是我想避免的 - 否則,如果外接缺失(例如在別人的電腦上)電子表格本身仍然可以工作。

+0

感謝您的反饋,我試圖改善這個問題。我已經實例化類,但這是編譯器錯誤而不是運行時錯誤。感謝'implements'的鏈接,但是我對它的工作原理很滿意:) – crunch 2015-03-03 13:17:44

+0

那麼,當'Condition1'爲false時,你的代碼甚至不應該試圖編譯'Implements myAddIn.iInterfaceFoo'。根據加載項是否被加載,你有沒有把'Condition1'設置爲真/假的問題?可能看到[this](http://stackoverflow.com/q/15951518/2140173)和[that](http://stackoverflow.com/q/19726791/2140173) – 2015-03-03 14:34:11

+0

這就是它 - 它試圖編譯'實現'行,即使'Condition1'爲'False'。我會看看這些鏈接的答案。 – crunch 2015-03-03 15:39:20

回答

1

我當時看了很多,沒有找到一個很好的解決方案。

所以我在另一個文件中寫的有問題的功能,如果我需要它,我激活它是這樣的:

在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:\rvc\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