Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Conditional compilation with implements keyword

Tags:

excel

vba

Is it possible to use compiler conditional constants together with the "implements" keyword, where the interface is in an add-in?

I have the following in a class module in my workbook, let's call it book1:

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

I have the add-in myAddIn listed as a reference (i.e. in Tools -> References...).

I'm successfully using the interface with other classes in the add-in, but now I want to call the interface directly in my workbook book1. As long as the add-in is open, when I compile book1 (i.e. Debug -> Compile VBAProject) it compiles successfully.

However, when I try to compile book1 with the add-in closed, I get the error

Compile error: User-defined type not defined

This is precisely what I'm trying to avoid - otherwise if the add-in is missing (for example on someone else's computer) the spreadsheet itself will still work.

like image 709
crunch Avatar asked Mar 02 '15 16:03

crunch


1 Answers

I was looking a lot and didn't find a good solution to that problem.

So i wrote the problematic function in another file, and if i need it i activate it like this:

On sp.mdb in a module:

Public Function soap30object() As Object
Set soap30object = New SoapClient30
End Function

On the main file:

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

Have fun!


Another solution

Replace the code in Modul on Runtime...

     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
like image 168
Zvi Redler Avatar answered Nov 08 '22 20:11

Zvi Redler