Search code examples
pythonvbacomwinreg

Python COM server with VBA late biding + skip win register (no admin rights)


I'm trying to import Python code into in VBA.

The code below works but requires admin rights. Is there a way to go around the win register need (assume I just don't have admin rights) but keep the 'late biding' behavior (dont want to Tools>>Reference every time I compile something new)

class ProofOfConcept(object):
    def __init__(self):
        self.output = []

    def GetData(self):
        with open('C:\Users\MyPath\Documents\COMs\SourceData.txt') as FileObj:
            for line in FileObj:
                self.output.append(line)
            return self.output

class COMProofOfConcept(object):
    _reg_clsid_ = "{D25A5B2A-9544-4C07-8077-DB3611BE63E7}"
    _reg_progid_= 'RiskTools.ProofOfConcept'
    _public_methods_ = ['GetData']

def __init__(self):
    self.__ProofOfConcept = ProofOfConcept()

def GetData(self):
    return self.__ProofOfConcept.GetData()

if __name__=='__main__':
    print "Registering COM server..."
    import win32com.server.register
    win32com.server.register.UseCommandLine(COMProofOfConcept)

VBA Code that calls it:

Sub TestProofOfConcept()
    Set PoF = CreateObject("RiskTools.ProofOfConcept")
    x = PoF.GetData()
    MsgBox x(0)
End Sub

Solution

  • In short, no. The VBA runtime basically uses the CoGetClassObject COM API under the hood - the CreateObject() function is essentially just a thin wrapper around it (it calls CLSIDFromString to locate the CLSID from the parameter first). Both of these functions require that the class be registered.