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

瘦欲@ 提交于 2019-12-23 01:18:09

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/39175926/python-com-server-with-vba-late-biding-skip-win-register-no-admin-rights

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!